使用十六进制数初始化无符号字节数组

时间:2012-02-11 15:24:45

标签: java byte unsigned

我知道 Java 中缺少无符号字节 那么如何使用 0到255(十六进制)中的整数初始化字节数组?

    final byte assoc_resp_msg_int[] = new byte[] {
            0xe3, 0x00, //APDU CHOICE Type(AareApdu)
            0x00, 0x2c, //CHOICE.length = 44
            0x00, 0x00, //result=accept
            0x50, 0x79, //data-proto-id = 20601
            0x00, 0x26, //data-proto-info length = 38
            0x80, 0x00, 0x00, 0x00, //protocolVersion
            0x80, 0x00, //encoding rules = MDER
            0x80, 0x00, 0x00, 0x00, //nomenclatureVersion
            0x00, 0x00, 0x00, 0x00, //functionalUnits, normal Association
            0x80, 0x00, 0x00, 0x00, //systemType = sys-type-manager
            0x00, 0x08, //system-id length = 8 and value (manufacturer- and device- specific) 
            0x88, 0x77, 0x66, 0x55, 0x44, 0x33, 0x22, 0x11,
            0x00, 0x00, //Manager's response to config-id is always 0
            0x00, 0x00, //Manager's response to data-req-mode-flags is always 0
            0x00, 0x00, //data-req-init-agent-count and data-req-init-manager-count are always 0
            0x00, 0x00, 0x00, 0x00, //optionList.count = 0 | optionList.length = 0
    };

1 个答案:

答案 0 :(得分:17)

您必须将0x80存储在这样的字节中:

final byte assoc_resp_msg_int[] = new byte[] {
        (byte)0xe3, 0x00, //APDU CHOICE Type(AareApdu)
        0x00, 0x2c, //CHOICE.length = 44
        0x00, 0x00, //result=accept
        0x50, 0x79, //data-proto-id = 20601
        0x00, 0x26, 
        (byte)0x80,
...
}
System.out.println(assoc_resp_msg_int[10]&0xFF);
//128