468,738 Members | 2,392 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,738 developers. It's quick & easy.

How to convert a byte array to a singe integer

Hi all,

Let's suppose we have this:

byte[] buffer = new byte[3];

buffer[0] = 0x04;
buffer[1] = 0xF1;
buffer[2] = 0xB4;

int Res;
'Res' needs to be the concatenation of all those bytes.
I mean, it should be something like

Res = 0x04F1B4

which converted to integer is 324020.

On the other hand, buffer can contain any number of bytes,not only 3
(I know the length)
I tried
Res = Int32.Parse(buffer[0].ToString() + buffer[1].ToString() +
buffer[2].ToString());

but obviously it doesn't work

Any ideas?

Thanks!

May 7 '07 #1
5 13454
Star <st**@nospam.comwrote:
Let's suppose we have this:

byte[] buffer = new byte[3];

buffer[0] = 0x04;
buffer[1] = 0xF1;
buffer[2] = 0xB4;

int Res;
'Res' needs to be the concatenation of all those bytes.
I mean, it should be something like

Res = 0x04F1B4

which converted to integer is 324020.
<snip>

I don't know of anything which will work with arbitrary numbers of
bytes, but BitConverter is usually used for converting between bytes
and other types such as ints etc. However, you need 4 bytes to convert
to an int, 8 to convert to a long etc. Still, without knowing your real
use case, it might be useful.

(If the endianness is a problem, I have a version which allows you to
specify the endianness - see http://pobox.com/~skeet/csharp/miscutil)

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 7 '07 #2
On Mon, 07 May 2007 15:09:20 -0700, Star <st**@nospam.comwrote:
[...]
'Res' needs to be the concatenation of all those bytes.
I mean, it should be something like

Res = 0x04F1B4

which converted to integer is 324020.

On the other hand, buffer can contain any number of bytes,not only 3
(I know the length)
Pedantic mode: obviously the buffer cannot contain *any* number of bytes..
It has to be within the range of the number of bytes used to represent the
destination type. :)

Anyway, that said, as an example of how you might use the BitConverter
class that Jon mentioned:

static public int Convert(byte[] rgbInput)
{
byte[] rgbWork = new byte[4];

if (rgbInput.Length rgbWork.Length)
{
throw new ArgumentOutOfRangeException("Maximum length of
rgbInput is " + rgbWork.Length);
}

rgbInput.CopyTo(rgbWork, 0);

return BitConverter.ToInt32(rgbWork, 0);
}

Note that the code assumes your destination type is in fact an int, and
that the data is little-endian. The example you gave actually shows
big-endian data. To handle big-endian, you might add this line just
before the one calling the CopyTo() method:

Array.Reverse(rgbInput);

Hope that helps.

Pete
May 7 '07 #3
Thanks for your replies.

Yes, BitConverter should work for me if I have 2, 4 or 8 bytes.
However, what should I call if I have 3 bytes?

May 8 '07 #4
On Tue, 08 May 2007 08:20:06 -0700, Star <st**@nospam.comwrote:
Thanks for your replies.

Yes, BitConverter should work for me if I have 2, 4 or 8 bytes.
However, what should I call if I have 3 bytes?
Did you look at the code example I posted? You can pass an array of any
length, up the maximum length of the destination type. Including "3
bytes" for an int.
May 8 '07 #5
oh, sorry about that, Peter!

I'll take a look. Thanks

Peter Duniho wrote:
On Tue, 08 May 2007 08:20:06 -0700, Star <st**@nospam.comwrote:
>Thanks for your replies.

Yes, BitConverter should work for me if I have 2, 4 or 8 bytes.
However, what should I call if I have 3 bytes?

Did you look at the code example I posted? You can pass an array of any
length, up the maximum length of the destination type. Including "3
bytes" for an int.
May 9 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

9 posts views Thread by mprocopio | last post: by
2 posts views Thread by Mel WEaver | last post: by
9 posts views Thread by Charles Law | last post: by
4 posts views Thread by msosno01 | last post: by
3 posts views Thread by =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
xarzu
2 posts views Thread by xarzu | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.