Solved

Byte Array to Integer

Posted on 1998-04-12
3
533 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
servlet example 11 76
check java version using powershell 13 470
How  can  i  resolve  HTTP Status 404 -? 8 79
running on tomcat not jboss eap 7.0 3 53
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn about the different types of variables in Java and how to declare them. Decide the type of variable desired: Put the keyword corresponding to the type of variable in front of the variable name: Use the equal sign to assign a v…
This video teaches viewers about errors in exception handling.
Suggested Courses

739 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