0

I am using C++ on Arduino.

Suppose I have a stream of binary data;

binary data: 0xFF, 0x00, 0x01, 0xCC

I want to convert it to the ASCII equivalent and store it in a String object type.

The converted string should look like this "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.

2
  • May be too slow or is too slow? Commented Jan 14, 2016 at 6:12
  • 1
    #define hex_char(c) (((c)>9)?'A'+(c)-10:'0'+(c)); myString += hex_char((buffer[i]>>4)&0xf); myString += hex_char((buffer[i] & 0xf)); or something like that... Commented Jan 14, 2016 at 6:13

3 Answers 3

2

My guess would be that the String class resizes each time a text is appended, that could be improved. 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';
Sign up to request clarification or add additional context in comments.

Comments

1

There is stringstream class available in C++, it may be usable in this case. 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).

#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;
}

1 Comment

Probably wont work on arduino (a free standing C or C++ implementation, not a hosted one)
1

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[]?).

Consider the following example with stringstream class (just include sstream and iomanip standard headers):

    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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.