473,398 Members | 2,165 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,398 software developers and data experts.

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 13690
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

19
by: jeff | last post by:
how do you convert form byte to Int32 while retaining the binary value of the byte array
9
by: mprocopio | last post by:
Fellow JavaScripters, I am looking for code or implementation ideas for converting an integer variable to a four-byte array. I'm porting some of my code from C#, where I make use of their...
2
by: Mel WEaver | last post by:
Hello, I have the following delphi structure for a binary file. I'm looking for idea how to read this file. Mel type TMenuDataStruct = packed record exename : string;
2
by: antonio matos | last post by:
hi people. I have to acess a database, and there are some fields that are binary. when i acess that field is read like an array of bytes. but in reality it's an array of doubles!!! how can...
9
by: Charles Law | last post by:
Suppose I have a structure Private Structure MyStruct Dim el1 As Byte Dim el2 As Int16 Dim el3 As Byte End Structure I want to convert this into a byte array where
14
by: Charles Law | last post by:
I thought this had come up before, but I now cannot find it. I have a byte array, such as Dim a() As Byte = {1, 2, 3, 4} I want to convert this to an Int32 = 01020304 (hex). If I use...
6
by: moondaddy | last post by:
I'm writing an app in vb.net 1.1 and need to convert a byte array into a string, and then from a string back to a byte array. for example Private mByte() as New Byte(4){11,22,33,44} Now how...
4
by: msosno01 | last post by:
I have Java client that connects to C++ server. The client sends integer in binary using DataOutputStream write function. I am reading these data into buffer. I have to convert this buffer back...
3
by: =?utf-8?B?Qm9yaXMgRHXFoWVr?= | last post by:
Hi, I am looking for the best way to convert a string of length 1 (= 1 character as string) to integer that has the same value as numeric representation of that character. Background: I am...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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...
0
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...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.