[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
Solved

# How to convert a byte[] to an integer

Posted on 2002-03-28
Medium Priority
264 Views
I have a byte[] with four bytes!
for example:
byte [] k={23,39,23,40}
how to convert k[] to an integer?
0
Question by:liluqun
[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
• 3
• 2

LVL 16

Expert Comment

ID: 6904262
For LittleEndian:

byte[] k={23,39,23,40};
int y=0;

y=k[0];
for (int i=1; i<4; i++) {
y = y << 8;
y += k[i];
}

For BigEndian:
byte[] k={23,39,23,40};
int y=0;

y=k[3];
for (int i=2; i>=0; i--) {
y = y << 8;
y += k[i];
}

0

LVL 9

Expert Comment

ID: 6904583
Or, not worying about manual conversion, like

byte[] byteArray = {1, 2, 3, 4, 4, 5, 6};
int value = -1; // -1 considered as invalid value for this operation.
try {
value = Integer.parseInt(new String(byteArray));
} catch(Exception e) {
value = -1;
}
0

LVL 16

Expert Comment

ID: 6904597
Ovi, your method does not work.
0

LVL 9

Expert Comment

ID: 6904607
Sorry about that, I've tested myself after I posted. Please ignore the comment.
0

Author Comment

ID: 6904635
Now try this code:
===========================================
import java.io.*;

public class Test {

public Test() {
}
public static void main(String[] args) {
Test test1 = new Test();
try {
RandomAccessFile rf=new RandomAccessFile("d:\\ok.txt","rw");
rf.writeInt(777678);
rf.close();
}
catch (Exception ex) {
System.out.print(ex);
}

}
}
===============================================
After run the above code , we can get a 4-bytes file!
Now we use bytes arrarys to restore the integer777678,
I know use rf.readInt() can restore the integer 777687 at easy,this time ,suppose I can get a byte arrary from the 4-bytes file ok.txt
Now retore it with the following code:
===============================================
import java.io.*;

public class Test {

public Test() {
}
public static void main(String[] args) {
Test test1 = new Test();
try {
RandomAccessFile rf=new RandomAccessFile("d:\\ok.txt","rw");
// rf.writeByte();
//rf.writeInt(777678);
byte [] w=new byte[4];
int y=0;

y=w[0];
for (int i=1; i<4; i++) {
y = y << 8;
y += w[i];
}

System.out.print(y);
rf.close();
}
catch (Exception ex) {
System.out.print(ex);
}

}
}
============================
we can only get: an integer:711886
not 777678!!
Why
0

LVL 16

Accepted Solution

Peter Kwan earned 60 total points
ID: 6904659
This is because "byte" is 8-bit 2's complement.

Here is a working version:

if (w[0] < 0)
y += 256;       // 2^8 = 256
y=w[0];
for (int i=1; i<4; i++) {
y = y << 8;
if (w[i] < 0)
y+=256;
y += w[i];
}
0

## Featured Post

Question has a verified solution.

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

Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
###### Suggested Courses
Course of the Month14 days, 21 hours left to enroll