java java中是否可以有一个未签名的ByteBuffer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9883472/
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
Is it possible to have an unsigned ByteBuffer in java?
提问by user697111
Subject says it all. I'm working with OpenGL and OpenCL and would make life easier if I could just use an unsigned ByteBuffer to store data.
对象说明了一切。我正在使用 OpenGL 和 OpenCL,如果我可以使用未签名的 ByteBuffer 来存储数据,那会让生活更轻松。
回答by kandarp
unsigned ByteBuffer example:
无符号字节缓冲区示例:
import java.nio.ByteBuffer;
public class test {
public static short getUnsignedByte(ByteBuffer bb) {
return ((short) (bb.get() & 0xff));
}
public static void putUnsignedByte(ByteBuffer bb, int value) {
bb.put((byte) (value & 0xff));
}
public static short getUnsignedByte(ByteBuffer bb, int position) {
return ((short) (bb.get(position) & (short) 0xff));
}
public static void putUnsignedByte(ByteBuffer bb, int position, int value) {
bb.put(position, (byte) (value & 0xff));
}
// ---------------------------------------------------------------
public static int getUnsignedShort(ByteBuffer bb) {
return (bb.getShort() & 0xffff);
}
public static void putUnsignedShort(ByteBuffer bb, int value) {
bb.putShort((short) (value & 0xffff));
}
public static int getUnsignedShort(ByteBuffer bb, int position) {
return (bb.getShort(position) & 0xffff);
}
public static void putUnsignedShort(ByteBuffer bb, int position, int value) {
bb.putShort(position, (short) (value & 0xffff));
}
// ---------------------------------------------------------------
public static long getUnsignedInt(ByteBuffer bb) {
return ((long) bb.getInt() & 0xffffffffL);
}
public static void putUnsignedInt(ByteBuffer bb, long value) {
bb.putInt((int) (value & 0xffffffffL));
}
public static long getUnsignedInt(ByteBuffer bb, int position) {
return ((long) bb.getInt(position) & 0xffffffffL);
}
public static void putUnsignedInt(ByteBuffer bb, int position, long value) {
bb.putInt(position, (int) (value & 0xffffffffL));
}
// ---------------------------------------------------
public static void main(String[] argv) throws Exception {
ByteBuffer buffer = ByteBuffer.allocate(20);
buffer.clear();
test.putUnsignedByte(buffer, 255);
test.putUnsignedByte(buffer, 128);
test.putUnsignedShort(buffer, 0xcafe);
test.putUnsignedInt(buffer, 0xcafebabe);
for (int i = 0; i < 8; i++) {
System.out.println("" + i + ": "
+ Integer.toHexString((int) getUnsignedByte(buffer, i)));
}
System.out.println("2: "
+ Integer.toHexString(getUnsignedShort(buffer, 2)));
System.out.println("4: " + Long.toHexString(getUnsignedInt(buffer, 4)));
}
}
回答by John3136
Java doesn't support unsigned types. The typical solution is to go to the next biggest type (in your case: short), and just mask it so you only use the lower 'n' (in your case 8) bits.
Java 不支持无符号类型。典型的解决方案是使用下一个最大的类型(在您的情况下:short),然后将其屏蔽,以便您仅使用较低的“n”(在您的情况下为 8)位。
... but that kind of breaks when you try to apply to buffers :-(
...但是当您尝试应用于缓冲区时会中断:-(
回答by Andreas Dolk
It's not a problem of ByteBuffer
- even if it was unsigned- every byte that you read from it will be signed, just because byte
is signed and we can't change that.
这不是问题ByteBuffer
- 即使它是未签名的- 您从中读取的每个字节都将被签名,只是因为byte
已签名并且我们无法更改它。