Converting array data

  • Thread starter Thread starter Pete Davis
  • Start date Start date
P

Pete Davis

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
 
pete, i'm not sure on your question, but i'll try to answer what i think
you're asking. please correct me if i'm not answering the right question :)

are you asking about doing the actual byte arithmetic or the best way to
access the array? if you're coming from a c background and you want to use
consecutive array elements as "memory chunks", it's not quite so nice. i
think your best bet is going to be a simple walk of the array, and using
the standard shift operators like c has. for instance, to convert 4 bytes
of a byte array to an int, you could write a simple routine:

public int ByteToInt(byte [ ] b, int startIndex)
{
int i = 0;
for(int j = 0; j < 4; j++)
i = (i << 8) + b[ j + startIndex ];
return i;
}

then you can use that to loop through all the bytes in the array...let me
know if i can help or if that's not what you're lookin for....

jeff.

--

This posting is provided "AS IS" with no warranties, and confers no rights.
Use of included script samples are subject to the terms specified at
http://www.microsoft.com/info/cpyright.htm

Note: For the benefit of the community-at-large, all responses to this
message are best directed to the newsgroup/thread from which they
originated.
 
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
 
Mathew,
Wow, thanks for the myriad of options. That's about the most complete
answer I've ever gotten on a programming newsgroup. Fortunately, I think the
MemoryStream will be sufficient for my needs. I'm not going to worry about
performance for the moment, but it's nice to knwo I have options.

Pete
--
http://www.petedavis.net

Matthew W. Jackson said:
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
 
Back
Top