Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert uint8_t* to std::string in C++? [duplicate]

Tags:

c++

string

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.

like image 423
Lincoln Avatar asked Dec 22 '10 11:12

Lincoln


2 Answers

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.

like image 89
sbi Avatar answered Nov 11 '22 08:11

sbi


Is your uint8* string null-terminated? If so, you can just do:

std::string mystring(data);
like image 1
Oliver Charlesworth Avatar answered Nov 11 '22 07:11

Oliver Charlesworth