PC Review


Reply
Thread Tools Rate Thread

How to convert a byte array to a singe integer

 
 
Star
Guest
Posts: n/a
 
      7th May 2007
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!



 
Reply With Quote
 
 
 
 
Jon Skeet [C# MVP]
Guest
Posts: n/a
 
      7th May 2007
Star <(E-Mail Removed)> wrote:
> 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 - <(E-Mail Removed)>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      8th May 2007
On Mon, 07 May 2007 15:09:20 -0700, Star <(E-Mail Removed)> wrote:

> [...]
> '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
 
Reply With Quote
 
Star
Guest
Posts: n/a
 
      8th May 2007
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?

 
Reply With Quote
 
Peter Duniho
Guest
Posts: n/a
 
      8th May 2007
On Tue, 08 May 2007 08:20:06 -0700, Star <(E-Mail Removed)> wrote:

> 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.
 
Reply With Quote
 
Star
Guest
Posts: n/a
 
      9th May 2007
oh, sorry about that, Peter!

I'll take a look. Thanks

Peter Duniho wrote:
> On Tue, 08 May 2007 08:20:06 -0700, Star <(E-Mail Removed)> wrote:
>
>> 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.

 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Convert Integer to Byte Array? Christian Reizlein Microsoft VB .NET 1 19th May 2008 03:20 AM
converting a byte array to an integer =?Utf-8?B?Vmlua2k=?= Microsoft C# .NET 3 5th Jun 2007 09:02 AM
Re: How to convert integer(long) to byte array? Larry Lard Microsoft VB .NET 1 7th Jul 2005 06:07 AM
Re: Convert Integer to Byte Array Jon Skeet [C# MVP] Microsoft Dot NET 0 27th Jul 2004 07:37 PM
hex byte array to integer David Glover Microsoft C# .NET 0 8th Jul 2003 11:36 AM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 07:39 AM.