?
Solved

Converting Byte Arrays to an Integer

Posted on 2007-08-09
4
Medium Priority
?
350 Views
Last Modified: 2010-03-30
In java I have a four byte array that contains the representation of an integer in network byte order. How can I convert this from a byte array to an integer? For example, the byte array might contain the following hex values "00 00 ff ff" which represents 65535. Another example is the hex value "00 00 00 80" represents 128. I thnk what is making this difficult is that converting from a byte to an integer results in a signed integer not an unsigned integer.
0
Comment
Question by:anickles
  • 3
4 Comments
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 150 total points
ID: 19666910
>>I thnk what is making this difficult is that converting from a byte to an integer results in a signed integer not an unsigned integer.

Well Java doesn't have unsigned integers

int i = new BigInteger(byteArray).intValue();
0
 
LVL 4

Accepted Solution

by:
doraemon-nolife earned 225 total points
ID: 19668047
You can achieve the unsigned integer length using long

int (32 bit)
long (64 bit)  unsigned integer(should be 64 bit if it exist in java, but not now !)


borrow and modify a bit of CEHJ code:

long val = new BigInteger(byteArray).longValue();

:) Hope this help.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19668540
>>
borrow and modify a bit of CEHJ code:

long val = new BigInteger(byteArray).longValue();
>>

No - that won't work as it will still be interpreted as a signed value.

long val = new BigInteger(1, byteArray).longValue();
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 19668897
:-)

Sorry - i assumed you knew there is a long in Java
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

There are many Password Managers (PM) out there to choose from. PM's can help with your password habits and routines, but they should not be a crutch you rely on too heavily. I also have an article for company/enterprise PM's.
Will you be ready when the clock on GDPR compliance runs out? Is GDPR even something you need to worry about? Find out more about the upcoming regulation changes and download our comprehensive GDPR checklist today !
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
Suggested Courses

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question