Sandah Aung
Sandah Aung

Reputation: 6188

Map unsigned byte to Java types (JNI)

I have declared this native method.

public  native  int SentApduToIcCard(byte[] apdu, int apdulen, byte[]data);

In native code, byte is unsigned. However, in Java unsigned bytes are not available.

I need to send values outside the byte range of Java to the native code. Eg:

    sendBuff[0] = 0x00;
    sendBuff[1] = 0xa4;
    sendBuff[2] = 0x04;
    sendBuff[3] = 0x00;
    sendBuff[4] = 0x0b;
    sendBuff[5] = 0xa0;
    sendBuff[6] = 0x00;
    sendBuff[7] = 0x00;
    sendBuff[8] = 0x05;
    sendBuff[9] = 0x82;
    sendBuff[10] = 0x00;
    sendBuff[11] = 0x00;
    sendBuff[12] = 0x00;
    sendBuff[13] = 0x00;
    sendBuff[14] = 0x00;
    sendBuff[15] = 0x02;

Is there a workaround to this problem?

Upvotes: 0

Views: 199

Answers (1)

apangin
apangin

Reputation: 98370

Just cast values to byte, and this will work as expected.

sendBuff[1] = (byte) 0xa4;

Upvotes: 2

Related Questions