Java convert Byte Array of ASCII values to int

After reading a ByteBuffer into b_array I am trying to convert the ascii values to int.

Output I am expecting is 129 after executing the code as (b_array[] has the decimal equivalents of ascii codes 49,50,59)

Can some one please tell me where am I doing wrong here. I am doing a 0xFF to make it a unsigned value in java and then OR operation to move the bytes.

        byte[]  b_array=new byte[3];            
        buffer.get(b_array,0,3);            
        // Contents inside the b_array in ascii code
        //  b_array[0]=49;
        //  b_array[1]=50;
        //  b_array[2]=57;       

        int value=  b_array[2] & 0xFF | (b_array[1] & 0xFF) << 8 | (b_array[0] & 0xFF) << 16;       
        System.out.println(value);
Jon Skeet
people
quotationmark

Your current approach is effectively treating the three values as a 24-bit number - effectively 0x313239.

It sounds like you should be converting it into a string, then parsing that:

String text = new String(b_array, StandardCharsets.US_ASCII); // "129"
int value = Integer.parseInt(text);

people

See more on this question at Stackoverflow