byte conversion

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
 
N

Nicholas Paldino [.NET/C# MVP]

Lew,

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

Hope this helps.
 
L

Lew Barnesson

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
 
N

Nicholas Paldino [.NET/C# MVP]

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
 
L

Lew Barnesson

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top