Re: binary conversion
Niall Gallagher <[email protected]>
| Newsgroups | gmane.comp.java.sun.servlet |
|---|---|
| Message-ID | <[email protected]> |
>In the example that you gave it,
>you are converting an integer
>into big and small endian right?
>
>I guess what I tried to ask was
>I have a 4 byte of data,
>and it is not stored in ascci format
>but rather in byte data only, so I have
>to read in data as byte array like this...
>
>byte num [] = new byte[4]
Hi
What you have is a list of 8-bit bytes and what you want to do
is to convert these into a single integer. So for example if
the integer was 1 you have the equivelant of
a [0000 0001]
b [0000 0000]
c [0000 0000]
d [0000 0000]
Where a is num[0] and d is num[3]. Well if I remember big
endian right what you want is the binary 32-bit integer
[0000 0000 0000 0000 0000 0000 0000 0001]
So to do this the following expression should work.
int bigE = ((num[0] & 0x00)
| ((num[1] & 0x00) << 8)
| ((num[2] & 0x00) << 16)
| ((num[3] & 0x00) << 24));
This should give you the correct integer value, However
I may have my big and little endian upside down, in which
case the following sould work
int bigE = ((num[3] & 0x00)
| ((num[2] & 0x00) << 8)
| ((num[1] & 0x00) << 16)
| ((num[0] & 0x00) << 24));
I have had to do this many times before, so you should
have no problem with these expressions, if you do, let me know.
Niall
___________________________________________________________________________
To unsubscribe, send email to [email protected] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html