How to get an integer value of byte array?

Reply

Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

How to get an integer value of byte array?

 
0
  #1
Jan 1st, 2009
I have a byte array has follows:

0 6 -56 28

How can I get its integer value by doing a manual calculation?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #2
Jan 1st, 2009
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Sep 2008
Posts: 34
Reputation: srs_grp is an unknown quantity at this point 
Solved Threads: 0
srs_grp srs_grp is offline Offline
Light Poster

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

 
0
  #3
Jan 1st, 2009
Actually I want 2 convert it into integer manually.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #4
Jan 1st, 2009
then you will have to use your own binary to decimal function and pass it the byte value.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

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

 
0
  #5
Jan 3rd, 2009
You have a couple of options:
- Use the ByteBuffer class (link is to a tutorial I wrote in case helpful), for example:

  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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #6
Jan 3rd, 2009
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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

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

 
0
  #7
Jan 3rd, 2009
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!
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #8
Jan 3rd, 2009
Array of Byte means
  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.
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 53
Reputation: neilcoffey will become famous soon enough neilcoffey will become famous soon enough 
Solved Threads: 6
neilcoffey neilcoffey is offline Offline
Junior Poster in Training

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

 
0
  #9
Jan 3rd, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 822
Reputation: verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough verruckt24 is a jewel in the rough 
Solved Threads: 73
verruckt24's Avatar
verruckt24 verruckt24 is offline Offline
Practically a Posting Shark

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

 
0
  #10
Jan 4th, 2009
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 !!
Get up every morning and take a look at the Forbes' list of richest people. If your name doesn't appear.... GET TO WORK !!!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC