Arduino:uint8_t 数组到字符串 [英] Arduino: uint8_t array to string

查看:120
本文介绍了Arduino:uint8_t 数组到字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个基于 android 的 NFC 应用程序,它发送一个哈希作为 apdu 答案.这是我在 Android 应用中用于发送哈希的代码:

I have an NFC application built on android that sends a hash as an apdu answer. This is the code I use in my Android app to send the hash:

@Override
    public byte[] processCommandApdu(byte[] arg0, Bundle arg1) {

        String hash = "e68d3f574009cbbe011150263634c5c0";

        return hash.getBytes(Charset.forName("UTF-8"));

    }

现在,当我在 Arduino 方面收到它时,我得到了这个 RAW 数据:

Now when I receive it on the Arduino side of things I get this RAW data:

10154561005110253555248485799989810148494949534850255255255255255255255255255

我如何从中获取哈希值?

How do I get the hash back from that?

这就是我现在所拥有的,但显然不起作用:

This is what I have right now but it's obviously not working:

        uint8_t response[32];

        uint8_t responseLength = sizeof(response);

        if (nfc.inDataExchange(message, sizeof(message), response, &responseLength)) {

            Serial.print("RAW: ");
            for (int i = 0; i < sizeof(response); i++) {
                Serial.print(response[i]);
            }

            Serial.println(" ");

            char buffer[32];
            itoa((int)response,buffer,8);

            Serial.print("ITOA: ");
            for (int i = 0; i < sizeof(buffer); i++) {
                Serial.print(buffer[i]);
            }

            Serial.println(" ");
       }

这是上面代码的串行输出:

And this is the serial output of the code above:

RAW: 10154561005110253555248485799989810148494949534850255255255255255255255255255 
ITOA: 4253   µ      + 
  3ü       R    

呸!!!

推荐答案

三个建议,但没有一个真正解释为什么最后几个字节被截断:

Three suggestions, though none of them really explains why the last few bytes get truncated:

  1. 不要将十六进制哈希表示转换为字符串,以便以后以 UTF-8 编码发送这些字符.直接将哈希作为字节发送会更有效(并且解码工作更少):

  1. 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
public byte[] 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;
}

如果您已经将哈希作为十六进制字符串,我建议您先在 Android 端将其转换为字节表示.

If you already have the hash as a hexadecimal string, I suggest you convert it to its byte representation on the Android side first.

使用 HCE 时,您应该坚持使用 ISO/IEC 7816-4 APDU,而不仅仅是发送随机数据.命令 APDU(短格式)包括以下内容:

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       |
| (1 Byte) | (1 Byte) | (1 Byte) | (1 Byte) | (1 Byte) | (Lc Bytes) | (1 Byte) |
+----------+----------+----------+----------+----------+------------+----------+

其中 Lc 编码 DATA 的字节数.如果 DATA 为空,则 Lc 也为空.Le 编码预期作为响应的字节数(Le = 0x00 的特殊情况,这意味着预期的响应字节数为 256.

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.

响应 APDU(这是您在 processCommandApdu 中作为返回值发送的内容)如下所示:

A response APDU (that's what you send as a return value in processCommandApdu) looks like this:

+----------+----------+----------+
| DATA     | SW1      | SW2      |
| (n Byte) | (1 Byte) | (1 Byte) |
+----------+----------+----------+

DATA 是响应数据.SW1 &SW2 形成响应状态字(通常 SW1 = 0x90,SW2 = 0x00 表示成功).请注意,SW1 和 SW2 是必需的.

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.

当遍历 inDataExchange 的响应时,使用该函数提供的响应长度 (responseLength) 而不是您的最大缓冲区长度:

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) {
    ...
}

此外,我建议您提供一个超过最大预期响应长度的缓冲区.(特别是在您对 32 个字符的字符串使用 UTF-8 编码的情况下,这可能(对于某些字符)导致超过 32 个字节.)

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.)

这篇关于Arduino:uint8_t 数组到字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆