-1

I have a byte[] like this

byte[] buffer = new byte[1024];

this byte[] may have values like this:

buffer = {0, 0, 0, 106, 0, 0, 0, 11, 64, 33, 50, 32, 32, 32, ....}

I am trying to get first 8 bytes, that is:

0,0,0,106

0,0,0,11

, and convert these into integers which is 106 and 11.

I can safely assume that first 8 bytes always represent 2 integers like in example above, these are 106 and 11 and that they take form of 4 bytes with 1st 3 being 0's like above.

Both are 4 byte signed integers in Hi-Lo order

How do I do that in C#?

cd491415
  • 823
  • 2
  • 14
  • 33
  • @GiladGreen - only when the triple zeroes are also guaranteed. – H H Feb 20 '19 at 17:59
  • Are we assuming these are 4-byte integers, most significant bytes first? – Joe Sewell Feb 20 '19 at 18:02
  • .net has the `BinaryReader` https://learn.microsoft.com/en-us/dotnet/api/system.io.binaryreader?view=netframework-4.7.2 – Daniel A. White Feb 20 '19 at 18:04
  • @DanielA.White - I would expect the same endiannes as BitConverter. – H H Feb 20 '19 at 18:06
  • @DanielA.White, BinaryReader reads integers in little-endian format. OP specifically mentions that the integer octets/bytes are in Hi-Lo order (big-endian order) –  Feb 20 '19 at 18:17
  • 1
    @elgonzo i linked this in my answer. https://stackoverflow.com/questions/8620885/c-sharp-binary-reader-in-big-endian – Daniel A. White Feb 20 '19 at 18:18
  • Yeah, just saw your answer now. My apologies for my superfluous comment :-D –  Feb 20 '19 at 18:19

5 Answers5

3

All you need is to access indices 3 and 7:

int first = buffer[3];
int second = buffer[7];

There is an implicit conversion from byte to int.

This is possible due to the following:

I can safely assume that [...] they take form of 4 bytes with 1st 3 being 0's

Therefore you only need the last byte of each 4-byte integer.

Callum Watkins
  • 2,844
  • 4
  • 29
  • 49
3

A simple DIY function:

int BytesToInt32(byte[] buff, int offset)
{
    return (buff[offset + 0] << 24)
         + (buff[offset + 1] << 16)
         + (buff[offset + 2] << 8)
         + (buff[offset + 3]);
}

and then:

buffer = {0, 0, 0, 106, 0, 0, 0, 11, 64, 33, 50, 32, 32, 32, ....};
int a = BytesToInt32(buffer, 0);
int b = BytesToInt32(buffer, 4);
H H
  • 263,252
  • 30
  • 330
  • 514
2

I would convert your byte[] into a MemoryStream (or leave it as a Stream). Then use a BinaryReader as appropriate. If the endianness isn't correct: C# - Binary reader in Big Endian?

Daniel A. White
  • 187,200
  • 47
  • 362
  • 445
1
private static int BytesToInt(byte[] array, int startIndex){
    int toReturn = 0;
    for (int i = startIndex; i < startIndex + 4; i++)
    {
        toReturn = toReturn << 8;
        toReturn = toReturn + array[i];
    }
    return toReturn;
}
Joe Sewell
  • 6,067
  • 1
  • 21
  • 34
0

Use the convert class.

int myint1 = Convert.ToInt32(buffer[someIndex1]);
int myint2 = Convert.ToInt32(buffer[someIndex2]);

As others mentioned, if it's guaranteed that index 4 and index 7 has the non-zero bytes then you can just plug it in straight.

GDogg
  • 41
  • 2