Arduino: Uint8_t Array To String
Solution 1:
Three suggestions, though none of them really explains why the last few bytes get truncated:
Don't convert the hexadecimal hash representation to a character string to later send those characters in UTF-8 encoding. It would be much more efficient (and less decoding effort) to directly send the hash as bytes:
@Override publicbyte[] processCommandApdu(byte[] arg0, Bundle arg1) { byte[] hash = { (byte)0xe6, (byte)0x8d, (byte)0x3f, (byte)0x57, (byte)0x40, (byte)0x09, (byte)0xcb, (byte)0xbe, (byte)0x01, (byte)0x11, (byte)0x50, (byte)0x26, (byte)0x36, (byte)0x34, (byte)0xc5, (byte)0xc0 }; return hash; }
If you already have the hash as a hexadecimal string, I suggest you convert it to its byte representation on the Android side first.
When using HCE, you should stick to ISO/IEC 7816-4 APDUs instead of just sending random data. A command APDU (short format) consists of the following:
+----------+----------+----------+----------+----------+------------+----------+ | CLA | INS | P1 | P2 | Lc | DATA | Le | | (1Byte) | (1Byte) | (1Byte) | (1Byte) | (1Byte) | (Lc Bytes) | (1Byte) | +----------+----------+----------+----------+----------+------------+----------+
Where Lc encodes the number of bytes of DATA. If DATA is empty, Lc is empty too. Le encodes the number of bytes expected as response (with the special case of Le = 0x00, which means 256 response bytes expected.
A response APDU (that's what you send as a return value in
processCommandApdu
) looks like this:+----------+----------+----------+ | DATA | SW1 | SW2 | | (n Byte) | (1Byte) | (1Byte) | +----------+----------+----------+
DATA is the response data. SW1 & SW2 form the response status word (typically SW1 = 0x90, SW2 = 0x00 for success). Note that SW1 and SW2 are mandatory.
When iterating through the response of
inDataExchange
use the response length provided by that function (responseLength
) instead of your maximum buffer length:for (int i = 0; i < responseLength; ++i) { ... }
Moreover, I suggest that you provide a buffer with more than the maximum expected response length. (Particularly in your case where you use UTF-8 encoding for a 32 character string, which might (for some characters) result in more that 32 bytes.)
Solution 2:
OK so I figured out my answer. I don't need itoa. I can just typecast the RAW input to char and get what I need:
Serial.print("TYPECASTED RAW: ");
for (int i = 0; i < sizeof(response); i++) {
Serial.print((char)response[i]);
}
Serial.println(" ");
And that outputed:
e68d3f574009cbbe0111502ÿÿÿÿÿÿÿÿÿ
Now I just wonder why the last 9 characters are being replaced with 255?
Post a Comment for "Arduino: Uint8_t Array To String"