Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialize unsigned byte array using hex number

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
    };
like image 286
Bear Avatar asked Feb 11 '12 15:02

Bear


People also ask

How do you convert bytes to hexadecimal?

To convert byte array to a hex value, we loop through each byte in the array and use String 's format() . We use %02X to print two places ( 02 ) of Hexadecimal ( X ) value and store it in the string st . This is a relatively slower process for large byte array conversion.

How many hex is 4 bytes?

Byte to Hexadecimal. The bytes are 8 bit signed integers in Java. Therefore, we need to convert each 4-bit segment to hex separately and concatenate them. Consequently, we'll get two hexadecimal characters after conversion.

How do you create a dummy byte array in Java?

The java. util. Random. nextBytes() method generates random bytes and provides it to the user defined byte array.

How do I print a Bytearray?

You can simply iterate the byte array and print the byte using System. out. println() method.


1 Answers

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
like image 75
alain.janinm Avatar answered Sep 20 '22 20:09

alain.janinm