Possible Duplicate:
How to Convert Byte* to std::string in C++?
I'm on an embedded device and try to receive a message.
This message is given by a const uint8_t* data
and its length size_t len
.
Now I need a std::string
to output my data.
Possible Duplicate:
How to Convert Byte* to std::string in C++?
I'm on an embedded device and try to receive a message.
This message is given by a const uint8_t* data
and its length size_t len
.
Now I need a std::string
to output my data.
If you don't want to convert the encoding, this will work:
std::string s( data, data+len );
If you want to convert UTF-8 into whatever system encoding is used by your platform, you need to use some platform-specific means.
Is your uint8*
string null-terminated? If so, you can just do:
std::string mystring(data);
For some case sizeof(uint8) != sizeof(char), you could do:
std::string output( len, 0 );
for ( size_t i = 0; i < len; ++i )
output[ i ] = static_cast<char>( data[ i ] );