2012-03-27 17 views

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.

+3

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

+0

Scrivere un metodo di utilità proprio facendo & 0xff è spazzatura –

+0

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 –

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