c++ - Way to get unsigned char into a std::string without reinterpret_cast? -


I have an unsigned four array that I need in std :: string, but my current method uses reinterpret_cast To whom I want to escape Is there a cleaner way to do this?

  Unsigned four my_txt [] = {0x52, 0x5f, 0x73, 0x68, 0x7e, 0x29, 0x33, 0x74, 0x74, 0x73, 0x72, 0x55} unsigned int my_txt_len = 12; Std :: string my_std_string (reinterpret_cast & lt; const char * & gt; (my_txt), my_txt_len);  

Use an Iterator Constructor:

  std :: String my_std_string (my_txt, my_txt + my_txt_len);  

It seems that you want to convert unsigned characters into four. If you want redefine them, then you should use the reinterpret_cast , it will be completely clean, because what you say is exactly done. .

In your example, however, it does not matter because all the values ​​in your array are within the range of 0 to CHAR_MAX . Therefore, it is a guarantee that those values ​​are displayed in the same way as char because they are in the unsigned char , and therefore referencing them is the same as converting them is. If you have a higher price then CHAR_MAX is permitted to behave differently to apply them.


Comments