473,513 Members | 2,403 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

byte conversion

Hi,

I have a byte array read from a file using Stream IO, and containing a
mixture of 16-bit integers, strings, etc..

The problem: what is the best way to convert two byte sequences into 16-bit
integers in a way similar to what I used to do with C or C++ formatted IO?

Is there really no direct way to accomplish this without my having to write
my own formatted IO routines? Any help appreciated.

Thanks.

- Lew
Dec 18 '06 #1
4 1689
Lew,

You can use the static ToInt16 method on the BitConverter class to do
this.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lew Barnesson" <le*****@centurytel.netwrote in message
news:b8******************************@centurytel.n et...
Hi,

I have a byte array read from a file using Stream IO, and containing a
mixture of 16-bit integers, strings, etc..

The problem: what is the best way to convert two byte sequences into
16-bit integers in a way similar to what I used to do with C or C++
formatted IO?

Is there really no direct way to accomplish this without my having to
write my own formatted IO routines? Any help appreciated.

Thanks.

- Lew

Dec 18 '06 #2
Nicholas,

Thank you. Exactly what I needed.

On another issue, do you have any counterpart for the C 'Union' construct in
C#?

- Lew
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:%2******************@TK2MSFTNGP04.phx.gbl...
Lew,

You can use the static ToInt16 method on the BitConverter class to do
this.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lew Barnesson" <le*****@centurytel.netwrote in message
news:b8******************************@centurytel.n et...
>Hi,

I have a byte array read from a file using Stream IO, and containing a
mixture of 16-bit integers, strings, etc..

The problem: what is the best way to convert two byte sequences into
16-bit integers in a way similar to what I used to do with C or C++
formatted IO?

Is there really no direct way to accomplish this without my having to
write my own formatted IO routines? Any help appreciated.

Thanks.

- Lew


Dec 18 '06 #3
Lew,

There really isn't one. You could use the FieldOffset attribute, but
for really complex unions, you will probably have to do something custom.

I would look up the terms "union" and "FieldOffset" in groups.google.com
in order to see previous threads on this subject.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lew Barnesson" <le*****@centurytel.netwrote in message
news:Yf******************************@centurytel.n et...
Nicholas,

Thank you. Exactly what I needed.

On another issue, do you have any counterpart for the C 'Union' construct
in C#?

- Lew
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2******************@TK2MSFTNGP04.phx.gbl...
>Lew,

You can use the static ToInt16 method on the BitConverter class to do
this.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lew Barnesson" <le*****@centurytel.netwrote in message
news:b8******************************@centurytel. net...
>>Hi,

I have a byte array read from a file using Stream IO, and containing a
mixture of 16-bit integers, strings, etc..

The problem: what is the best way to convert two byte sequences into
16-bit integers in a way similar to what I used to do with C or C++
formatted IO?

Is there really no direct way to accomplish this without my having to
write my own formatted IO routines? Any help appreciated.

Thanks.

- Lew



Dec 18 '06 #4
Nicholas,

Thanks. Good advice. I'll Google (Group) it. :-)

"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote in
message news:ut**************@TK2MSFTNGP04.phx.gbl...
Lew,

There really isn't one. You could use the FieldOffset attribute, but
for really complex unions, you will probably have to do something custom.

I would look up the terms "union" and "FieldOffset" in
groups.google.com in order to see previous threads on this subject.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lew Barnesson" <le*****@centurytel.netwrote in message
news:Yf******************************@centurytel.n et...
>Nicholas,

Thank you. Exactly what I needed.

On another issue, do you have any counterpart for the C 'Union' construct
in C#?

- Lew
"Nicholas Paldino [.NET/C# MVP]" <mv*@spam.guard.caspershouse.comwrote
in message news:%2******************@TK2MSFTNGP04.phx.gbl...
>>Lew,

You can use the static ToInt16 method on the BitConverter class to do
this.

Hope this helps.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard.caspershouse.com

"Lew Barnesson" <le*****@centurytel.netwrote in message
news:b8******************************@centurytel .net...
Hi,

I have a byte array read from a file using Stream IO, and containing a
mixture of 16-bit integers, strings, etc..

The problem: what is the best way to convert two byte sequences into
16-bit integers in a way similar to what I used to do with C or C++
formatted IO?

Is there really no direct way to accomplish this without my having to
write my own formatted IO routines? Any help appreciated.

Thanks.

- Lew



Dec 18 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

13
7873
by: Bryan Parkoff | last post by:
I have two variables: "char A" and "short B". I can be able to convert from A to B using explicit case conversion with no problem like "B = short (A);". Right now, I have two variables: "char T"...
3
8419
by: mailar | last post by:
HI, Can anyone tell me how is a multi_byte to single byte and vice versa conversion done in DB2. It would be great even if someone can tell me how Oracle does it? Oracle already has functions...
2
4522
by: Jesse Engle | last post by:
i'm learning how to do some basic low-level network programming. the site i'm reading talks about "network byte order" and "host byte order". the thing is, it doesn't give an explanation as to what...
11
10127
by: QQ | last post by:
I know a char is 2 bytes, the conversion is like byte byte_array = new byte; //Allocate double mem as that of char then for each char do byte = (byte) char & 0xff byte = (byte)( char >> 8 &...
1
32778
by: Wasim Akram | last post by:
Hi, I have a field "Month" in my SQL server table. The type of this field is "tinyint". Now what I am doing in the code is using DataRow to read this field in a 'int' variable. int month...
4
2403
by: Dennis Myrén | last post by:
Hi. Is there a way to utilize the great primitive data type formatting routines available in .NET without working with strings? I want a byte directly rather than a string. I think it is...
3
12857
by: megan | last post by:
I can easily get my byte array into the SqlBinary Sql servr field. getting it out again is more difficult. SqlBinary myBin = myBinaryObject; // object from SQL server field. byte b= myBin ; ...
25
7221
by: Charles Law | last post by:
I thought this was going to be straight forward, given the wealth of conversion functions in .NET, but it is proving more convoluted than imagined. Given the following <code> Dim ba(1) As...
16
2126
by: Hugh Janus | last post by:
Hi all, I am using the below functions in order to convert strings to bytes and vice versa. I totally ans shamefully stole these functions from this group btw! Anyway, they work great but as...
5
5949
by: jeremyje | last post by:
I'm writing some code that will convert a regular string to a byte for compression and then beable to convert that compressed string back into original form. Conceptually I have.... For...
0
7264
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7166
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
7106
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7534
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5094
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4749
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3236
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3226
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1601
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.