views:

269

answers:

5

I have a control that has a byte array in it.

Every now and then there are two bytes that tell me some info about number of future items in the array.

So as an example I could have:

...
...
Item [4] = 7
Item [5] = 0
...
...

The value of this is clearly 7.

But what about this?

...
...
Item [4] = 0
Item [5] = 7
...
...

Any idea on what that equates to (as an normal int)?

I went to binary and thought it may be 11100000000 which equals 1792. But I don't know if that is how it really works (ie does it use the whole 8 items for the byte).

Is there any way to know this with out testing?

Note: I am using C# 3.0 and visual studio 2008

+3  A: 

A two-byte number has a low and a high byte. The high byte is worth 256 times as much as the low byte:

value = 256 * high + low;

So, for high=0 and low=7, the value is 7. But for high=7 and low=0, the value becomes 1792.

This of course assumes that the number is a simple 16-bit integer. If it's anything fancier, the above won't be enough. Then you need more knowledge about how the number is encoded, in order to decode it.

The order in which the high and low bytes appear is determined by the endianness of the byte stream. In big-endian, you will see high before low (at a lower address), in little-endian it's the other way around.

unwind
Also worth noting: `(high << 8) | low`
GeReV
A: 

If those bytes are the "parts" of an integer it works like that. But beware, that the order of bytes is platform specific and that it also depends on the length of the integer (16 bit=2 bytes, 32 bit=4bytes, ...)

Danvil
A: 

Use BitConveter.ToInt32 im not 100% sure that would give you the right result but you could try

Petoj
+2  A: 

You say "this value is clearly 7", but it depends entirely on the encoding. If we assume full-width bytes, then in little-endian, yes; 7, 0 is 7. But in big endian it isn't.

For little-endian, what you want is

int i = byte[i] | (byte[i+1] << 8);

and for big-endian:

int i = (byte[i] << 8) | byte[i+1];

But other encoding schemes are available; for example, some schemes use 7-bit arithmetic, with the 8th bit as a continuation bit. Some schemes (UTF-8) put all the continuation bits in the first byte (so the first has only limited room for data bits), and 8 bits for the rest in the sequence.

Marc Gravell
Good point. But from the usage I can see that the value is 7 (there are seven more sections of coordinates in the array.
Vaccano
+1  A: 

BitConverter can easily convert the two bytes in a two-byte integer value:

// assumes byte[] Item = someObject.GetBytes():
short num = BitConverter.ToInt16(Item, 4); // makes a short 
    // out of Item[4] and Item[5]
MusiGenesis