Convert Bytes to bits

13,098

Solution 1

I'm not a Java developer so this could be completely off-base, but have you considered using a ByteBuffer?

Solution 2

Assume the LSB is at data[0]

int val;

val = (((int)data[0]) & 0x00FF) | ((int)data[1]<<8);

Solution 3

easier way in Java to parse an array of bytes to bits is JBBP usage

  class Parsed { @Bin(type = BinType.BIT_ARRAY) byte [] bits;}
  final Parsed parsed = JBBPParser.prepare("bit:1 [_] bits;").parse(theByteArray).mapTo(Parsed.class);

the code will place parsed bits of each byte as 8 bytes in the bits array of the Parsed class instance

Solution 4

ByteInputStream b = new ByteInputStream(audioData);
DataInputStream data = new DataInputStream(b);
short value = data.readShort();

The advantage of the above code is that you can keep reading the rest of 'data' in the same way.

A simpler solution for just two values might be:

short value = (short) ((audioData[0]<<8) | (audioData[1] & 0xff));

This simple solution extracts two bytes, and pieces them together with the first byte being the higher order bits and the second byte the lower order bits (this is known as Big-Endian; if your byte array contained Little-Endian data, you would shift the second byte over instead for 16-bit numbers; for Little-Endian 32-bit numbers, you would have to reverse the order of all 4 bytes, because Java's integers follow Big-Endian ordering).

Solution 5

As suggested before, Java has classes to help you with this. You can wrap your array with a ByteBuffer and then get an IntBuffer view of it.

ByteBuffer bb = ByteBuffer.wrap(audioData);
// optional: bb.order(ByteOrder.BIG_ENDIAN) or bb.order(ByteOrder.LITTLE_ENDIAN)
IntBuffer ib = bb.asIntBuffer();
int firstInt = ib.get(0);
Share:
13,098
dedalo
Author by

dedalo

Updated on June 15, 2022

Comments

  • dedalo
    dedalo about 2 years

    I'm working with java.

    I have a byte array (8 bits in each position of the array) and what I need to do is to put together 2 of the values of the array and get a value.

    I'll try to explain myself better; I'm extracting audio data from a audio file. This data is stored in a byte array. Each audio sample has a size of 16 bits. If the array is:

    byte[] audioData;

    What I need is to get 1 value from samples audioData[0] and audioData[1] in order to get 1 audio sample.

    Can anyone explain me how to do this?

    Thanks in advance.

  • palantus
    palantus about 15 years
    It's not completely off-base.
  • StaxMan
    StaxMan about 15 years
    Nope, too simple; must use masking to avoid sign extension.
  • palantus
    palantus about 15 years
    ShortBuffer may be more appropriate here.
  • newacct
    newacct about 15 years
    this doesn't deal with the sign extension on byte 1
  • Artem Barger
    Artem Barger about 15 years
    the question was how to setup integer value while reading byte-byte. So whenever integer is sign extended it will stay the same, I'm completely not sure it the case there we have to take care about it.
  • Rob
    Rob over 11 years
    Don't you want something like bit[i] = ((myByte & (1 << i)) != 0);? I don't think this will work as written.