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
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