Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Bytes to bits

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.

like image 430
dedalo Avatar asked Jun 03 '09 19:06

dedalo


2 Answers

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

like image 152
Adam Robinson Avatar answered Oct 08 '22 12:10

Adam Robinson


Assume the LSB is at data[0]

int val;

val = (((int)data[0]) & 0x00FF) | ((int)data[1]<<8);
like image 44
Artem Barger Avatar answered Oct 08 '22 12:10

Artem Barger