英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论