ostringstream - Convert array of uint8_t to string in C++ -


i have array of type uint8_t. want create string concatenates each element of array. here attempt using ostringstream, string seems empty afterward.

std::string key = ""; std::ostringstream convert; (int = 0; < key_size_; a++) {                                                  convert << key_arr[a]   key.append(convert.str()); }  cout << key << endl; 

try this:

std::ostringstream convert; (int = 0; < key_size_; a++) {     convert << (int)key[a]; }  std::string key_string = convert.str();  std::cout << key_string << std::endl; 

the ostringstream class string builder. can append values it, , when you're done can call it's .str() method std::string contains put it.

you need cast uint8_t values int before add them ostringstream because if don't treat them chars. on other hand, if represent chars, need remove (int) cast see actual characters.


edit: if array contains 0x1f 0x1f 0x1f , want string 1f1f1f, can use std::uppercase , std::hex manipulators, this:

std::ostringstream convert; (int = 0; < key_size_; a++) {     convert << std::uppercase << std::hex << (int)key[a]; } 

if want go decimal , lowercase, need use std::nouppercase , std::dec.


Comments

Popular posts from this blog

google chrome - Developer tools - How to inspect the elements which are added momentarily (by JQuery)? -

angularjs - Showing an empty as first option in select tag -

php - Cloud9 cloud IDE and CakePHP -