954,153 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to get an integer value of byte array?

I have a byte array has follows:

0 6 -56 28

How can I get its integer value by doing a manual calculation?

srs_grp
Light Poster
34 posts since Sep 2008
Reputation Points: 9
Solved Threads: 0
 

If you have an array of Byte() class, then the class itself has an intValue() method that returns the value of this Byte as an int.
Read the javadocs for the Byte class for more details.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Actually I want 2 convert it into integer manually.

srs_grp
Light Poster
34 posts since Sep 2008
Reputation Points: 9
Solved Threads: 0
 

then you will have to use your own binary to decimal function and pass it the byte value.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

You have a couple of options:
- Use the ByteBuffer class (link is to a tutorial I wrote in case helpful), for example:

ByteBuffer bb = ByteBuffer.allocate(4);
bb.put((byte) ...);
bb.put((byte) ...);
bb.put((byte) ...);
bb.put((byte) ...);
int intVal = bb.getInt(0);


- More efficiently, just manually code it with shifts and ORs. You also generally have to be careful to AND each byte value with 0xff to remove the sign:

int intVal = ((b0 & 0xff) << 24) | ((b1 & 0xff) << 16) | (b2 & 0xff) << 8) | (b3 & 0xff);

For anunsigned int, use the above expression but store in a long. To reverse the endianness, obviously put b0-b3 the other way round.

neilcoffey
Junior Poster in Training
53 posts since Dec 2008
Reputation Points: 120
Solved Threads: 7
 

I said the same thing, the only thing is I left the logic part for him to figure out, and I feel thats what was supposed to be done.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Sorry, I really didn't understand "array of Byte() class" was supposed to tell the poster to do the above. I'm going to wager that they didn't either!

neilcoffey
Junior Poster in Training
53 posts since Dec 2008
Reputation Points: 120
Solved Threads: 7
 

Array of Byte means

Byte [] byteArray = new Byte [5];


I don't know how else to explain, but this certainly was picked up by the original poster since he asked me the next question on that, which told us he wanted to convert the bytes to integer manually. My response to this post "then you need to write your own binary to decimal function" explains the next step without going into unecesary details, which as I mentioned should be left to the OP to be figured out.

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

Ah, you mean convert each individual byte into an unsigned int. If that's really what the poster meant, then sorry -- I misinterpreted. But I think the poster means that they want to amalgamate the four bytes and get a single, four-byte integer value.

By the way, for converting a byte to an int, just ANDing with 0xff is common and efficient.

neilcoffey
Junior Poster in Training
53 posts since Dec 2008
Reputation Points: 120
Solved Threads: 7
 

On pondering over it now, I feel, even I might have misinterpreted, cause this 4 bytes thing didn't cross my mind, I was of the notion that he wanted the integer value of each of them. Well, nevermind, the OP has both the solutions, he can choose the appropriate one. Cheers !!

verruckt24
Posting Shark
952 posts since Nov 2008
Reputation Points: 485
Solved Threads: 89
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You