简体   繁体   English

将二进制十六进制数据转换为等效的ASCII并存储在String中

[英]Convert binary hex data to ASCII equivalent and store in String

I am using C++ on Arduino. 我在Arduino上使用C ++。

Suppose I have a stream of binary data; 假设我有二进制数据流;

binary data: 0xFF, 0x00, 0x01, 0xCC 二进制数据: 0xFF, 0x00, 0x01, 0xCC

I want to convert it to the ASCII equivalent and store it in a String object type. 我想将其转换为等效的ASCII并将其存储在String对象类型中。

The converted string should look like this "FF0001CC". 转换后的字符串应类似于此“ FF0001CC”。

Here are some draft code. 这是一些草稿代码。

char buffer[100];
String myString;

for (int i=0; i < numBytes; i++)
{
    //assume buffer contains some binary data at this point 
    myString += String(buffer[i], HEX);
}

The problem with this code is that myString contains FF01CC , not FF0001CC . 此代码的问题是myString包含FF01CC而不是FF0001CC

My guess would be that the String class resizes each time a text is appended, that could be improved. 我的猜测是,每当添加文本时,String类都会调整大小,这可以改进。 Assuming you know the input size and it´s constant, you could try this: 假设您知道输入大小并且它是恒定的,则可以尝试以下操作:

char outbuffer[numBytes*2+1];   
const char* pHexTable="0123456789ABCDEF";
int iPos=0;

for(int i=0; i<numBytes; i++){
    //assume buffer contains some binary data at this point
    const char cHex=buffer[i];
    outbuffer[iPos++]=pHexTable[(cHex>>4)&0x0f];
    outbuffer[iPos++]=pHexTable[cHex&0x0f];
}
outbuffer[iPos]='\0';

There is stringstream class available in C++, it may be usable in this case. C ++中有可用的stringstream类,在这种情况下可能可用。 With C three bytes would be printed to a buffer with one sprintf-statement sprintf(buffer, "%02x%02x%02x", bytes[0], bytes[1], bytes[2]) (preferably snprintf ). 对于C,三个字节将被打印到具有一个sprintf语句sprintf(buffer, "%02x%02x%02x", bytes[0], bytes[1], bytes[2]) (最好是snprintf )。

#include <sstream>
#include <iostream>
#include <iomanip>

int main(void)
{
    std::stringstream ss;
    unsigned char bytes[] = {0xff, 0x00, 0xcc};

    ss << std::hex;

    // This did not work, 00 was printed as 0
    // ss << std::setfill('0') << std::setw(2)
    // ...
    // ss << (unsigned int)bytes[i]

    for (int i=0; i<3; i++) {
       unsigned int tmp = bytes[i];
       ss << (tmp >> 4) << (tmp & 0xf);
    }
    std::cout << ss.str();

    return 0;
}

As understand numBytes can be bigger than 3 or 4 (otherwise why buffer size is 100?) Also I prefer to use C++ classes when working with string (you need string , not char[] ?). 据了解numBytes可以大于3或4(否则为什么缓冲区大小为100?)我也更喜欢在处理string时使用C ++类(您需要string ,而不是char[] ?)。

Consider the following example with stringstream class (just include sstream and iomanip standard headers): 考虑带stringstream类的以下示例(仅包括sstreamiomanip标准头):

    string myString;
    stringstream myStringStream;
    myStringStream << setbase(16);
    myStringStream << uppercase;
    for (int i = 0; i < numBytes; i++)
    {
        myStringStream << (0xFF & (unsigned int) buffer[i]);
    }
    myString = myStringStream.str();

I can not compare the speed of my example with other answers, but this solution is really C++ approach for buffer of any size. 我无法将示例的速度与其他答案进行比较,但是此解决方案实际上是适用于任何大小的buffer的C ++方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM