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 the
world.

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

public static int getSignedIntegerBigEndianFromWord(byte[] word) {
...............
}

I looked at the BitConverter.GetBytes(), but this only works for
unsigned integers AFAIK.
Am i overlooking something?


Validation 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

ps: i just feel like i'm at school again, trying to solve such an easy
task :D

Eduard Witteveen
 
J

Jon Skeet [C# MVP]

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 the
world.

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

public static int getSignedIntegerBigEndianFromWord(byte[] word) {
...............

}

I looked at the BitConverter.GetBytes(), but this only works for
unsigned integers AFAIK.

No, BitConverter most definitely works with signed values. However,
it'll be little endian by default under Windows.

I have an "endian-specified" version of BitConverter as part of my
MiscUtil project - see
http://pobox.com/~skeet/csharp/miscutil

By the way, if you're converting 2 bytes into an integer (rather than
4) then "short" would be a more appropriate return type from your
method.

Jon
 
E

eywitteveen

I have an "endian-specified" version of BitConverter as part of my
MiscUtil project - seehttp://pobox.com/~skeet/csharp/miscutil
<snip />
If i would have to use the whole library, i'm quite not sure which
class and methods to choose. Could you be a little specific about
which functions i have to use.
 
J

Jon Skeet [C# MVP]

<snip />
If i would have to use the whole library, i'm quite not sure which
class and methods to choose. Could you be a little specific about
which functions i have to use.

I would have thought a quick look down the list of contents would have
been enough:

"MiscUtil.Conversion.EndianBitConverter - effectively BitConverter, but
either LittleEndian or BigEndian, depending on which you pick."
 
E

eywitteveen

I would have thought a quick look down the list of contents would have
been enough:
"MiscUtil.Conversion.EndianBitConverter - effectively BitConverter, but
either LittleEndian or BigEndian, depending on which you pick."
I still do not know how to convert the buffer into a integer value.
I've now tried different approaches, but i've got a feeling that this
library could be quite usefull to solve the problem, but it doesnt
solve the question i've asked.
I thought that i created a simple and easy to understand question, if
the question is not clear enough im happy to refrase.

Eduard
 
J

Jon Skeet [C# MVP]

I still do not know how to convert the buffer into a integer value.
I've now tried different approaches, but i've got a feeling that this
library could be quite usefull to solve the problem, but it doesnt
solve the question i've asked.

Yes, it does.
I thought that i created a simple and easy to understand question, if
the question is not clear enough im happy to refrase.

Just as with BitConverter, you want a UInt16 (a 16 bit unsigned
integer) so you call ToUInt16 (byte[] value, int startIndex).
 

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