Solved

Byte Array to Integer

Posted on 1998-04-12
3
538 Views
Last Modified: 2009-07-29
I have a byte[] array of size 4 which I want to convert into a 4-byte int or Integer. What method should I use to do this?

I have tried:
String key = new String(bArray, 0, 3);
int result = Integer.valueOf(key).intValue();

which does not seem to work.

Any help would be appreciated.
0
Comment
Question by:iwt
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 1

Expert Comment

by:peter_vc
ID: 1234052
I think you can just cast it to an Integer:

String key = new String(bArray,0,3);
int result = int(key);

I don't have my Java stuff installed on this machine so I can't test it.

0
 

Author Comment

by:iwt
ID: 1234053
No answer needed; I found a solution using the code from RandomAccessFile.java's WriteInt() and reversing it, which bypasses the need for a String.

For others' use, I've posted it below:

int result = ((bArray[0] << 24) | (bArray[1] << 16) + (bArray[2] << 8) + bArray[3]);

0
 
LVL 2

Accepted Solution

by:
anguslai earned 70 total points
ID: 1234054
Convert a byte array of 4 into 32 bits integer :

Assume :
     byte array named byteArray
     byte[0] contained the left most  8 bits (bit 25 to bit 32);
     
then use the following code to convert byteArray to int:
    int A = byteArray[0]<<24 | byteArray[1]<<16 | byteArray[2]<<8 | byteArray[3];

Note: the left most bit in primitive type int is a sign bit. Therefore, don't forget to treat the left most bit in byte[0] as sign bit.
0

Featured Post

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the third conditional statement “else if” and use it in an example program. Then additional information about conditional statements is provided, covering the topic thoroughly. Viewers learn about the third conditional statement …
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

630 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