7 years, 10 months ago.

Help with C++

Could someone help me with converting this vector to a string?

std::vector<uint8_t> devID = dot->getDeviceId();

Thank you.

1 Answer

7 years, 10 months ago.

The values stored in that vector are simply characters? In that case since a vector is continious stored (see: http://stackoverflow.com/questions/2923272/how-to-convert-vector-to-array-c), you can simply make a pointer to an array (which is I guess the type of string you mean), which points to the first element of that vector.

Accepted Answer

Looking at the mdot library getDeviceId() returns a vector that is 8 bytes long containing the ID. To me that would imply that it's an 8 byte long binary ID number rather than a null terminated ascii string.

It's not very elegant but in that situation the simplest option is probably

char devIDString[17];
std::vector<uint8_t> devID = dot->getDeviceId();
sprintf(devIDString,"%02X%02X%02X%02X%02X%02X%02X%02X",
                    devID[0],devID[1],devID[2],devID[3],
                    devID[4],devID[5],devID[6],devID[7]);

which will give string of the ID number in hex.

posted by Andy A 14 Apr 2016

Thats indeed then a better solution. Sometimes it is better to go for simply the solution that works than the most elegant one.

posted by Erik - 14 Apr 2016

Thank you Andy.

posted by Terrence Spencer 15 Apr 2016