在Java中是否有一种方法可以编写小于一个字节的比特组?

huangapple go评论67阅读模式
英文:

Is there a way to write groups of bits less than a byte in Java

问题

如果我要尝试在Java中创建一个输出.Flac文件的程序,Flac格式,似乎我需要输出小于一个字节的值。是否可能输出小于一个字节的值,比如半字节或5位数?

英文:

If I was to try to make a program that outputs .Flac files in Java, Flac-format,
It appears that I would need to output values less than a byte. Is it even possible to output values less than a byte such as a nybble or a 5-bit number?

答案1

得分: 1

ImageOutputStream.writeBits应该可以满足您的需求。

您可以使用ImageIO.createImageOutputStream方法创建一个ImageOutputStream:

Path flacPath = Paths.get(filename);

try (ImageOutputStream out = ImageIO.createImageOutputStream(
    new BufferedOutputStream(Files.newOutputStream(flacPath)))) {

    out.writeBits(0b1110101011, 10);
    // 等等。
}
英文:

ImageOutputStream.writeBits should do what you want.

You create an ImageOutputStream using the ImageIO.createImageOutputStream method:

Path flacPath = Paths.get(filename);

try (ImageOutputStream out = ImageIO.createImageOutputStream(
    new BufferedOutputStream(Files.newOutputStream(flacPath)))) {

    out.writeBits(0b1110101011, 10);
    // etc.
}

答案2

得分: 0

我认为最后我可以使用位移和逻辑命令,如 & 和 | 运算符来完成这个任务。

英文:

In the end I think I can do this using bit shifting and logical commands such as & and | opperators.

huangapple
  • 本文由 发表于 2020年8月1日 18:05:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/63204010.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定