There are several approaches you can take:
Take a look at these .NET Framework classes:
System.IO.BinaryReader -- for reading from a binary stream (has methods like
ReadInt32)
System.IO.MemoryStream -- you will create one of these out of your byte
array and read it with the BinaryReader
I would suggest these classes as it is relatively easy to understand and I
think it fits what you want to do the best.
-OR-
If those don't suit you then try these:
System.BitConverter -- for converting byte arrays to individual primatives
and back.
System.Buffer -- for copying arrays of one primative to arrays of another
primative.
System.Text.Encoding -- for converting byte arrays of various encodings to
Unicode strings and back.
(I would suggest this mode if you find the stream doesn't perform well,
although it should be good enough)
You could use BitConverter to convert to an int, which you would then cast
to your enum. Then you'd do the same for the next 4-bytes. If you had any
long series of values (like several ints in a row) that needed to be
converted to an array, use the Buffer class. If you need to read a string,
use the Encoding class.
-OR-
Last, and least:
unsafe pointers
(and I don't recommend this unless you're either insane or really worried
about performance)
You can use C#'s "unsafe" context to do things the way you would in C/C++
and access the memory directly with pointers.
(Footnote: If you are dealing with endian-specifc values and plan on ever
running your code on other platforms [e.g. Mono on MacOSX] you MAY want to
keep endian-ness in mind.)
--Matthew W. Jackson
"Pete Davis" <pd******@hotmail.com> wrote in message
news:96******************************@news.meganet news.com...
I've never done this in C# so I don't know what the appropriate way of
doing it is.
I've got an array of bytes and I need to convert the array into "usable"
data. For example, the first 4 bytes need to be converted to an enum. The
next 4 bytes to a 32-bit int. And so on. I'm basically populating a struct
with the data. How does one do this in C#?
Thanks.
Pete
--
http://www.petedavis.net