是否有可能在java中有一个无符号的ByteBuffer?

主题说明了一切。 我正在使用OpenGL和OpenCL,如果我可以使用无符号的ByteBuffer来存储数据,那么会让生活更轻松。

unsigned ByteBuffer示例:

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

Java不支持无符号类型。 典型的解决方案是转到下一个最大的类型(在您的情况下:short),然后屏蔽它,这样您只使用较低的’n’(在您的情况下为8)位。

…但是当你尝试申请缓冲时会出现这种中断:-(

这不是ByteBuffer的问题 – 即使它是无符号的 – 你从它读取的每个字节都将被签名,只是因为byte被签名而我们无法改变它。