How to convert a byte array to a singe integer

S

Star

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!
 
J

Jon Skeet [C# MVP]

Star said:
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)
 
P

Peter Duniho

[...]
'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
 
S

Star

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?
 
P

Peter Duniho

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top