Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I have 2 methods that one of them converts the Integer to byte array and the other one converts byte array to integer. But both of them when I define the length of byte array less than 4, I get an exception. How can I fix both of my methods.

public class Test {

public Test() {

}

public static int getIntegerFromByte(byte[] byteArr) {

        return (byteArr[0] ) << 24 | (byteArr[1] & 0xFF) << 16 | (byteArr[2] & 0xFF) << 8 | (byteArr[3] & 0xFF);

}

public byte[] getByteArrayFromInteger(int intValue ,int byteArrSize) {


            ByteBuffer wrapped = ByteBuffer.allocate(byteArrSize);
            wrapped.putInt(intValue);
            byte[] byteArray = wrapped.array();
            return byteArray;

}

public static void main (String []args) throws IOException {

    Test app = new Test();

    byte [] b = app.getByteArrayFromInteger(1, 3);
    System.out.println("Length of Byte:\t"+ b.length+ "\n Value: \t"+ getIntegerFromByte(b));
    byte [] byteArr = new byte[3];
    byteArr[0] = 0;
    byteArr[1] = 0;
    byteArr[2] = 1;

    System.out.println("Length of Byte:\t"+ byteArr.length+ "\n Value: \t"+ getIntegerFromByte(byteArr));

}

}

Thanks

share|improve this question
1  
Come on, spill the beans. Which exception? Also, is the endianness of int well-defined in Java, i.e. is your code portable? –  Bathsheba Oct 21 '13 at 12:35
    
Integer's occupy 32-bit. –  Dhrubajyoti Gogoi Oct 21 '13 at 12:35
    
@Cruncher: I think someone already gave the answer. The OP has a problem when there are less than 4 bytes. –  Dhrubajyoti Gogoi Oct 21 '13 at 12:39

6 Answers 6

An int is of 4 bytes. From the javadoc for ByteBuffer.putInt()

Writes four bytes containing the given int value, in the current byte order, into this buffer at the current position, and then increments the position by four.

and throws:

BufferOverflowException - If there are fewer than four bytes remaining in this buffer

share|improve this answer

Most likely you were trying to write a 32-bit (4 byte) int value to a 3 byte array. Your getIntegerFromByte expects an array of at least 4 bytes.

Try

byte[] b = app.getByteArrayFromInteger(1, 4);
share|improve this answer
    
But I want to change my integer in to 3 bytes. –  A R Oct 21 '13 at 12:43
    
You will have to write custom functions which writes/reads a 24-bit value. 24-bit int value is non-standard. The only library I know which does this is mine, but I wouldn't uses it just for this simple example. –  Peter Lawrey Oct 21 '13 at 12:45

this is due to fact that for conversion you are using byte array of length four, if you gives less size it will give exception relevant to out of bound....

share|improve this answer

try like this :-

byte [] b = app.getByteArrayFromInteger(0, 3);
share|improve this answer

Possible to write it something like this to get rid of the dependencies of length?

public static int getIntegerFromByte(byte[] byteArr) {
    return getIntegerFromByte(0, byteArr);

}

public static int getIntegerFromByte(int sum, byte[] byteArr) {
    if (byteArr.length == 0)
        return sum;
    sum = (sum << 8) + (byteArr[0] & 0xFF);
    return (getIntegerFromByte(sum, Arrays.copyOfRange(byteArr, 1, byteArr.length)));
}
public byte[] getByteArrayFromInteger(int intValue) {
    ByteBuffer wrapped = ByteBuffer.allocate(4);
    wrapped.putInt(intValue);
    return wrapped.array();
}

public static void main (String []args) throws IOException {

    Test app = new Test();

    byte [] b = app.getByteArrayFromInteger(2);
    System.out.println("Length of Byte:\t"+ b.length+ "\n Value: \t"+ getIntegerFromByte(b));
    byte [] byteArr = new byte[]{0,1,0};

    System.out.println("Length of Byte:\t"+ byteArr.length+ "\n Value: \t"+ getIntegerFromByte(byteArr));

}
share|improve this answer

You could more efficiently use the existing functionality of BigInteger as it does all this for you already.

public void test() {
  System.out.println("Hello");
  int i = 1234567;
  byte[] ba = BigInteger.valueOf(i).toByteArray();
  int j = new BigInteger(ba).intValue();
  System.out.println("i="+i+" ba="+Arrays.toString(ba)+" j="+j);
}

prints

i=1234567 ba=[18, -42, -121] j=1234567
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.