java 将字节转换为位

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/946593/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 14:31:05  来源:igfitidea点击:

Convert Bytes to bits

javatypesbytebitset

提问by dedalo

I'm working with java.

我正在使用 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.

我有一个字节数组(数组的每个位置 8 位),我需要做的是将数组的 2 个值放在一起并获得一个值。

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:

我会试着更好地解释自己;我正在从音频文件中提取音频数据。此数据存储在字节数组中。每个音频样本的大小为 16 位。如果数组是:

byte[] audioData;

字节 [] 音频数据;

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

我需要的是从样本 audioData[0] 和 audioData[1] 中获取 1 个值以获得 1 个音频样本。

Can anyone explain me how to do this?

谁能解释我如何做到这一点?

Thanks in advance.

提前致谢。

回答by Adam Robinson

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

我不是 Java 开发人员,所以这可能完全偏离基础,但是您是否考虑过使用ByteBuffer

回答by Artem Barger

Assume the LSB is at data[0]

假设 LSB 位于 data[0]

int val;

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

回答by deverton

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

正如之前所建议的,Java 有一些类可以帮助您解决这个问题。您可以使用ByteBuffer包装您的数组,然后获得它的IntBuffer视图。

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);

回答by Christopher

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).

这个简单的解决方案提取两个字节,并将它们拼凑在一起,第一个字节是高位,第二个字节是低位(这称为 Big-Endian;如果您的字节数组包含 Little-Endian 数据,您将移位对于 16 位数字,第二个字节结束;对于 Little-Endian 32 位数字,您必须颠倒所有 4 个字节的顺序,因为 Java 的整数遵循 Big-Endian 排序)。

回答by Igor Maznitsa

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

Java中将字节数组解析为位的更简单方法是JBBP 用法

  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

代码会将每个字节的解析位作为 8 个字节放置在 Parsed 类实例的位数组中

回答by Wilfred Springer

I suggest you take a look at Preon. In Preon, you would be able to say something like this:

我建议你看看Preon。在 Preon 中,您可以这样说:

class Sample {

  @BoundNumber(size="16") // Size of the sample in bits
  int value;

}

class AudioFile {

  @BoundList(size="...") // Number of samples
  Sample[] samples;

}

byte[] buffer = ...;
Codec<AudioFile> codec = Codecs.create(AudioFile.class);
AudioFile audioFile = codec.decode(buffer);

回答by Paul Morie

You can convert to a short(2 bytes) by logical or-ing the two bytes together:

您可以short通过将两个字节进行逻辑或运算来转换为 a (2 个字节):

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

回答by Buji Falcon

byte myByte = 0x5B;

boolean bits = new boolean[8];

for(int i = 0 ; i < 8 ; i++)
    bit[i] = (myByte%2 == 1);

The results is an array of zeros and ones where 1=TRUEand 0=FALSE:)

结果是一个由零和一组成的数组,其中1=TRUE0=FALSE:)