byte conversion

  • Thread starter Thread starter Lew Barnesson
  • Start date Start date
L

Lew Barnesson

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
 
Lew,

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

Hope this helps.
 
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 said:
Lew,

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

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lew Barnesson said:
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
 
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]
- (e-mail address removed)

Lew Barnesson said:
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 said:
Lew,

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

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

Lew Barnesson said:
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
 
Nicholas,

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

Nicholas Paldino said:
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]
- (e-mail address removed)

Lew Barnesson said:
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 said:
Lew,

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

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- (e-mail address removed)

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
 
Back
Top