Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / C# / September 2007

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
eywitteveen@gmail.com - 30 Sep 2007 15:30 GMT
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
Jon Skeet [C# MVP] - 30 Sep 2007 19:19 GMT
> 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).

Signature

Jon Skeet - <skeet@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Martin CLAVREUIL - 30 Sep 2007 20:23 GMT
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

> Hello,
>
[quoted text clipped - 49 lines]
>
> Eduard Witteveen
eywitteveen@gmail.com - 30 Sep 2007 21:11 GMT
> Wouldn't something (simple) like that do the job ?
>
[quoted text clipped - 8 lines]
>
> 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);
        }

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.