473,473 Members | 1,870 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Converting array data

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
Nov 15 '05 #1
3 2325
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.

Nov 15 '05 #2
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

Nov 15 '05 #3
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" <th********************@NOSPAM.NOSPAM> wrote in message
news:ut**************@TK2MSFTNGP09.phx.gbl...
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


Nov 15 '05 #4

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

Similar topics

5
by: matt melton | last post by:
Hi there, I am trying to write a method that accepts an array of any primitive type and will return the same array without copying memory as an array of bytes. ie. I'd like to be able to do...
4
by: Hal Vaughan | last post by:
If I have a byte and I convert it to string (String sData = new String(byte bData), then convert it back (byte bData = sData.getBytes()), will all data be intact, or do Strings have problems with...
2
by: Govind | last post by:
Hi All, I want to Convert 32 bit integers to byte in right alighed format . For 32 = the usual way is BitConverter.GetBytes(int32)==> xx xx 00 00 , but i want right aligned like 00 00 xx xx.Is...
4
by: Joseph Suprenant | last post by:
I have an array of unsigned chars and i would like them converted to an array of ints. What is the best way to do this? Using RedHat 7.3 on an Intel Pentium 4 machine. Having trouble here, hope...
1
by: Darrel | last post by:
I am using binary writer to write an array of bytes to disk. However, my data starts out as an array of sbytes. I am currently type casting each array element in a for loop. Is there a faster...
1
by: UKuser | last post by:
Hi Guys, I have a program which converts Excel spreadsheets to Javascript and allows interactivity. However it can't convert it to PHP, which is obviously better for users to view (in case J/S...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
9
by: Gregory.A.Book | last post by:
I am interested in converting sets of 4 bytes to floats in C++. I have a library that reads image data and returns the data as an array of unsigned chars. The image data is stored as 4-byte floats....
0
by: anide | last post by:
Hi all I’ve some problem, I’m trying to converting a sorting algorithm from C++ to C#. In C++ I’ve compiled it using MSVC and its working properly, and in C# I’m using .NET Framework 2.0 (Visual...
0
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
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
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
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
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
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
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 ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.