9

I know that unsigned byte is missing in Java Then how can I initialize the byte array using integer from 0 to 255 (in hex)?

    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
    };
Bear
  • 5,138
  • 5
  • 50
  • 80
  • 2
    Unsigned byte is possible in Java: `(int)mybyte & 0xFF` see further [here](http://stackoverflow.com/questions/4266756/can-we-make-unsigned-byte-in-java) – ecle Feb 11 '12 at 16:02
  • Eclipse properly indicates the lack of the byte cast, eclipse for the win! – A Person Mar 22 '13 at 07:29

1 Answers1

19

You have to store 0x80 in byte like this :

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
alain.janinm
  • 19,951
  • 10
  • 65
  • 112
  • 2
    Yes, you can store like this `(byte)255` as well – ecle Feb 11 '12 at 16:15
  • 1
    To test: `byte b = (byte) 255; byte c = (byte) 0xFF; if (c == b) System.out.println((b & 0xFF) + " == " + (c & 0xFF));` – ecle Feb 11 '12 at 16:22