Il soggetto dice tutto. Sto lavorando con OpenGL e OpenCL e renderebbe la vita più facile se potessi semplicemente utilizzare un ByteBuffer non firmato per memorizzare i dati.È possibile avere un ByteBuffer non firmato in java?
21
A
risposta
-2
Non è un problema di ByteBuffer
- anche se era unsigned - sarà firmata ogni byte che si legge da esso, proprio perché byte
è firmato e non possiamo cambiare la situazione.
51
unsigned esempio 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)));
}
}
1
Java non supporta i tipi senza segno. La soluzione tipica è quella di passare al tipo successivo più grande (nel tuo caso: breve), e basta mascherarlo in modo da utilizzare solo i bit inferiori 'n' (nel tuo caso 8).
... ma quel tipo di pause quando si tenta di applicare ai buffer :-(
Problemi correlati
- 1. non firmato corto in java
- 2. Domanda Java: è possibile avere un'istruzione switch in un altro?
- 3. Java ByteBuffer a String
- 4. Perché Java Web Start dice che un file jar firmato non è firmato?
- 5. Converti ByteBuffer in String in Java
- 6. Java/Android - Fast ByteBuffer Parsing
- 7. È possibile avere un ParameterExpression out?
- 8. Cancellazione rapida (non chiara) a ByteBuffer in Java
- 9. equivalente in C++ di Java ByteBuffer?
- 10. Converti ByteBuffer in byte array java
- 11. Java: è possibile avere riferimenti di classe finale reciproci?
- 12. Cancellazione di un ByteBuffer
- 13. Byte Java non firmato [2] su int?
- 14. È possibile avere specifiche parametrizzate in RSpec?
- 15. C89: disaccordo firmato/non firmato
- 16. Non firmare un jar firmato
- 17. È possibile avere dipendenze condizionali in gulp?
- 18. È possibile avere un semplice modulo libreria java in base all'SDK Android in Android Studio
- 19. In che modo il computer distingue un intero è firmato o non firmato?
- 20. È possibile avere una funzione di costruzione in un tratto?
- 21. È possibile avere un'app Facebook "di sfondo"?
- 22. Non è possibile creare un array di LinkedList in Java ...?
- 23. Come inserire il contenuto di un ByteBuffer in un OutputStream?
- 24. È possibile avere un alias per sys.stdout in python?
- 25. È possibile leggere il Process stdout InputStream in un ByteBuffer NIO?
- 26. Errore: Sfortunatamente non è possibile avere moduli Java non Gradle e> moduli Android-Gradle in un progetto
- 27. È possibile richiamare un println in Java?
- 28. In Java, è possibile cancellare un po '?
- 29. C'è un modo per creare un ByteBuffer diretto da un puntatore esclusivamente in Java?
- 30. E 'possibile avere IDE CDT e Java insieme in Eclipse?
Spazzatura. Byte byte. Che sono trattati come firmato importa solo quando segno estensione o li utilizzano come valori numerici. Tutto quello che devi fare è leggere i byte in un numero che può contenere più byte di quello che vuoi avere "unsigned" ('int' per' short's, 'long' per' int's, 'BigInteger' per 'long's, ecc.) – Thor84no
Scrivere un metodo di utilità proprio facendo & 0xff è spazzatura –
Finalmente ho trovato la bella classe in guava facendo esattamente quello che voglio http://docs.guava-libraries.googlecode.com/ git/javadoc/com/google/common/io/ByteArrayDataInput.html # readUnsignedByte% 28% 29 –