Important changes to forums and questions
All forums and questions are now archived. To start a new conversation or read the latest updates go to forums.mbed.com.
8 years, 8 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
8 years, 8 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.
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 14 Apr 2016