943,549 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 15384
  • Java RSS
Jan 1st, 2009
0

How to get an integer value of byte array?

Expand Post »
I have a byte array has follows:

0 6 -56 28

How can I get its integer value by doing a manual calculation?
Similar Threads
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Jan 1st, 2009
0

Re: How to get an integer value of byte array?

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 1st, 2009
0

Re: How to get an integer value of byte array?

Actually I want 2 convert it into integer manually.
Reputation Points: 9
Solved Threads: 0
Light Poster
srs_grp is offline Offline
34 posts
since Sep 2008
Jan 1st, 2009
0

Re: How to get an integer value of byte array?

then you will have to use your own binary to decimal function and pass it the byte value.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 3rd, 2009
0

Re: How to get an integer value of byte array?

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

Java Syntax (Toggle Plain Text)
  1. ByteBuffer bb = ByteBuffer.allocate(4);
  2. bb.put((byte) ...);
  3. bb.put((byte) ...);
  4. bb.put((byte) ...);
  5. bb.put((byte) ...);
  6. 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 an unsigned int, use the above expression but store in a long. To reverse the endianness, obviously put b0-b3 the other way round.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Jan 3rd, 2009
0

Re: How to get an integer value of byte array?

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.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 3rd, 2009
0

Re: How to get an integer value of byte array?

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!
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Jan 3rd, 2009
0

Re: How to get an integer value of byte array?

Array of Byte means
java Syntax (Toggle Plain Text)
  1. 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.
Last edited by verruckt24; Jan 3rd, 2009 at 2:34 pm.
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008
Jan 3rd, 2009
0

Re: How to get an integer value of byte array?

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.
Reputation Points: 120
Solved Threads: 7
Junior Poster in Training
neilcoffey is offline Offline
53 posts
since Dec 2008
Jan 4th, 2009
0

Re: How to get an integer value of byte array?

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 !!
Reputation Points: 485
Solved Threads: 89
Posting Shark
verruckt24 is offline Offline
944 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Help with Sorts
Next Thread in Java Forum Timeline: JTextArea





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC