How do i convert a byte[2] with format big endian signed int to a integer value?

E

eywitteveen

Hello,

Introduction:
I'm currently working on retrieving altitude values from a a
file(srtm). This file contains a array of byte values and depending of
the position i want to know, i have to read to byte's to know what the
altitude is.
This works fine, till i want to convert the bytes to a integer value,
representing the height on that specific place on theworld.
I've asked this question before on this list, but i got a pointer to a
library but this doesnt solve the problem i have.

The problem:
I have 2 bytes, which represents a signed integer (this means thevalue
can also be negative), so i want a function with the following
definition.

public static int getSignedIntegerFromBigEndian2Bytes(byte[] word) {
...
...
...
}
I looked at the BitConverter.GetInt16(), but this does not work for
the bytes i want to convert. In an earlier post Jon Skeet suggested to
use his MiscUtil project (which can be found on http://pobox.com/~skeet/csharp/miscutil),
especially the class MiscUtil.Conversion.BigEndianBitConverter and i
expect the function FromBytes only this doesnt return bytes or an
signed value.

I really expect that this problem could be solved within like 4 lines,
but i dont know where to start, so this is the reason why i turned to
this list. I am in the impression that the information i give is clear
enough, and otherwise feel free to ask for more information


Sample data:
byte[0] byte[1] 0-in bits 1-in bits word in
bits expected value
255 236 11111111 11101100
1111111111101100 -20
255 246 11111111 11110110
1111111111110110 -10
255 255 11111111 11111111
1111111111111111 -1
0 0 00000000 00000000
0000000000000000 0
0 1 00000000 00000001
0000000000000001 1
0 10 00000000 00001010
0000000000001010 10
0 20 00000000 00010100
0000000000010100 20

Eduard Witteveen
 
J

Jon Skeet [C# MVP]

This works fine, till i want to convert the bytes to a integer value,
representing the height on that specific place on theworld.
I've asked this question before on this list, but i got a pointer to a
library but this doesnt solve the problem i have.

Yes, it does. There's no need to start a new thread.
I looked at the BitConverter.GetInt16(), but this does not work for
the bytes i want to convert. In an earlier post Jon Skeet suggested to
use his MiscUtil project (which can be found on http://pobox.com/~skeet/csharp/miscutil),
especially the class MiscUtil.Conversion.BigEndianBitConverter and i
expect the function FromBytes only this doesnt return bytes or an
signed value.

EndianBitConverter is meant to be a direct replacement of BitConverter.
So as you wanted to use GetInt16 before, use GetInt16 with
EndiantBitConverter (except that it's ToInt16, not GetInt16). I don't
know where you got the impression that you had to use FromBytes, which
is protected anyway method (i.e. you couldn't call it anyway, without
deriving from the class).
 
M

Martin CLAVREUIL

Hi,

Wouldn't something (simple) like that do the job ?

foreach (TwoBytes sample in sampledata)
{
short result = (short)(((short)sample.Byte2) |
(short)(sample.Byte1 * 256));
Console.WriteLine("{0}|{1} =
{2}",sample.Byte1.ToString(),sample.Byte2.ToString(),result.ToString());
}
where byte1 is the left (index=0) one.

Hope it helps
 
E

eywitteveen

Wouldn't something (simple) like that do the job ?

foreach (TwoBytes sample in sampledata)
{
short result = (short)(((short)sample.Byte2) |
(short)(sample.Byte1 * 256));
Console.WriteLine("{0}|{1} =
{2}",sample.Byte1.ToString(),sample.Byte2.ToString(),result.ToString());
}
where byte1 is the left (index=0) one.

Hope it helps
Great thank you!
public static int getIntegerFromBytes(byte[] word) {
if(word.Length != 2) {
throw new ArgumentOutOfRangeException("word should have length 2,
but has a length of " + word.Length);
}
return (((short)word[1]) | (short)(word[0] * 256));
}
Works very nice!

I also managed to get the library working (after looking to the source
code and reposting the question) When one wants to use the library,
the following code should be used (so others stumbling uponthis thread
will find an answer).
public static int getIntegerFromBytes(byte[] word) {
if(word.Length != 2) {
throw new ArgumentOutOfRangeException("word should have length 2,
but has a length of " + word.Length);
}
MiscUtil.Conversion.EndianBitConverter converter =
MiscUtil.Conversion.EndianBitConverter.Big;
return converter.ToInt16(word,0);
}
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top