Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert vector of uint8 to string

I have a pointer to a vector of type uint8.

How would I take this pointer and convert the data in the vector into a full string representative of its content?

like image 795
Mr S Avatar asked Jul 31 '13 23:07

Mr S


1 Answers

You could just initialize the std::string with the sequence obtained from the std::vector<uint8_t>:

std::string str(v->begin(), v->end());

There is no need to play any tricks checking whether the std::vector<uint8_t> is empty: if it is, the range will be empty. However, you might want to check if the pointer is v is null. The above requires that it points to a valid object.

like image 65
Dietmar Kühl Avatar answered Sep 21 '22 07:09

Dietmar Kühl