Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to read an integer from a byte[]

Tags:

c#

.net

I have a byte array, and I would like to read an integer from this array. How can I do it?

Something like this:

 int i;

 tab = new byte[32];

 i = readint(tab,0,3); // i = int from tab[0] to tab[3] (int = 4 bytes?)

 i = readint(tab,4,7);

etc...

like image 672
Rodger Avatar asked Jun 16 '11 10:06

Rodger


4 Answers

byte[] bytes = { 0, 0, 0, 25 };

// If the system architecture is little-endian (that is, little end first),
// reverse the byte array.
if (BitConverter.IsLittleEndian)
    Array.Reverse(bytes);

int i = BitConverter.ToInt32(bytes, 0);
Console.WriteLine("int: {0}", i);

Ref: How to: Convert a byte Array to an int

like image 87
Mitch Wheat Avatar answered Sep 25 '22 02:09

Mitch Wheat


Also, there is a class called Endian in Jon Skeet's miscutil library which implements conversion methods between a byte array and various primitive types, taking endianness into account.

For your question, usage would be something like:

// Input data
byte[] tab = new byte[32];

// Pick the appropriate endianness
Endian endian = Endian.Little;

// Use the appropriate endian to convert
int a = endian.ToInt32(tab, 0);
int b = endian.ToInt32(tab, 4);
int c = endian.ToInt32(tab, 8);
int d = endian.ToInt32(tab, 16);
...

A simplified version of the Endian class would be something like:

public abstract class Endian
{
    public short ToInt16(byte[] value, int startIndex)
    {
        return unchecked((short)FromBytes(value, startIndex, 2));
    }

    public int ToInt32(byte[] value, int startIndex)
    {
        return unchecked((int)FromBytes(value, startIndex, 4));
    }

    public long ToInt64(byte[] value, int startIndex)
    {
        return FromBytes(value, startIndex, 8);
    }

    // This same method can be used by int16, int32 and int64.
    protected virtual long FromBytes(byte[] buffer, int startIndex, int len);
}

And then the FromBytes abstract method is implemented differently for each endian type.

public class BigEndian : Endian
{
    protected override long FromBytes(byte[] buffer, int startIndex, int len)
    {
        long ret = 0;
        for (int i=0; i < len; i++)
        {
            ret = unchecked((ret << 8) | buffer[startIndex+i]);
        }
        return ret;
    }
}

public class LittleEndian : Endian
{
    protected override long FromBytes(byte[] buffer, int startIndex, int len)
    {
        long ret = 0;
        for (int i=0; i < len; i++)
        {
            ret = unchecked((ret << 8) | buffer[startIndex+len-1-i]);
        }
        return ret;
    }
}
like image 29
Groo Avatar answered Sep 24 '22 02:09

Groo


You could use BitConverter.ToInt32. Have a look at this.

like image 23
FIre Panda Avatar answered Sep 26 '22 02:09

FIre Panda


If you wanted to do it manually, something like that should do the trick!

byte[] data = ...;
int startIndex = 0;
int value = data[startIndex];

for (int i=1;i<4;i++)
{
  value <<= 8;
  value |= data[i+startIndex];
}
like image 25
Kurru Avatar answered Sep 25 '22 02:09

Kurru