473,378 Members | 1,527 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

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 2317
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...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.