2012-03-27 2 views

답변

-2

그것은 ByteBuffer의 문제가 아니에요 -이 경우에도 부호 - 당신이 그것을 읽을 모든 바이트가 서명 될 것입니다 단지 때문에 byte에 서명하고 우리가 변경할 수 없습니다.

+3

쓰레기 :-(버퍼에 적용 할 때 브레이크의 종류. 바이트 바이트입니다. 서명으로 그들이 처리하고 그것은 단지 기호 확장하거나 숫자 값로 사용시 문제. 당신이해야 할 일은 바이트를 "unsigned"(int는'short's, long은'int's, BigInteger'는'unsigned ')보다 더 많은 바이트를 저장할 수 있습니다. 'long's 등) – Thor84no

+0

& 0xff를 수행하는 자신의 유틸리티 메소드를 작성하는 것은 쓰레기입니다. –

+0

마침내 구아바의 멋진 클래스가 내가 원하는 것을 정확하게 찾았습니다. http://docs.guava-libraries.googlecode.com/ git/javadoc/com/google/common/io/ByteArrayDataInput.html # readUnsignedByte % 28 % 29 –

51

부호의 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

자바 부호없는 형식을 지원하지 않습니다. 일반적인 해결책은 다음 가장 큰 유형 (귀하의 경우 : 짧음)으로 이동하여 마스크를 그냥 사용하여 더 낮은 'n'(귀하의 경우 8) 비트 만 사용하게하십시오.

...하지만 당신이

관련 문제