| | |
How to get an integer value of byte array?
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
![]() |
If you have an array of
Read the javadocs for the Byte class for more details.
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 !!!
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
You have a couple of options:
- Use the ByteBuffer class (link is to a tutorial I wrote in case helpful), for example:
- 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.
- Use the ByteBuffer class (link is to a tutorial I wrote in case helpful), for example:
Java Syntax (Toggle Plain Text)
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 an unsigned int, use the above expression but store in a long. To reverse the endianness, obviously put b0-b3 the other way round.
Array of
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.
Byte means java Syntax (Toggle Plain Text)
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 !!!
•
•
Join Date: Dec 2008
Posts: 53
Reputation:
Solved Threads: 6
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.
By the way, for converting a byte to an int, just ANDing with 0xff is common and efficient.
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 !!!
![]() |
Similar Threads
- Using VB Code - Silent download pdf from server and print to local printer (Visual Basic 4 / 5 / 6)
- How to create a byte array/structure (C++)
- filling a char array (C++)
- how to add the first element of first array of every record (Visual Basic 4 / 5 / 6)
- MIPS printing integer problem (Assembly)
- 2's complement of BYTE array in c++ or vc++ (C++)
- Coverting a string to a byte array (Visual Basic 4 / 5 / 6)
- NO value was pass over to it.. (VB.NET)
- How to make Java and C++ sockets play nice (Java)
Other Threads in the Java Forum
- Previous Thread: Help with Sorts
- Next Thread: need open sourse cell phone code
| Thread Tools | Search this Thread |
-xlint actionlistener android api applet application array arrays automation binary blackberry block bluetooth character chat class classes client code component consumer database desktop developmenthelp eclipse error event exception fractal ftp game gameprogramming givemetehcodez graphics gui html ide image input integer j2me j2seprojects java javac javaee javaprojects jmf jni jpanel julia lego linked linux list loop loops mac map method methods mobile netbeans newbie notdisplaying number online oracle print printf problem program programming project properties recursion researchinmotion rotatetext rsa scanner screen server set singleton size sms sort sql string swing system template textfields threads time title tree tutorial-sample update windows working





