17

I have an unsigned char array that I need in a std::string, but my current way uses reinterpret_cast which I would like to avoid. Is there a cleaner way to do this?

unsigned char 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<const char *>(my_txt), my_txt_len);
WilliamKF
  • 41,123
  • 68
  • 193
  • 295

3 Answers3

19

Use the iterator constructor:

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

This is assuming that you want the unsigned chars to be converted to char. If you want them to be reinterpreted, then you should use reinterpret_cast. That would be perfectly clean, since what you say is exactly what is done.

In your example, though, it doesn't make any difference, because all of the values in your array are within the range 0 to CHAR_MAX. So it's guaranteed that those values are represented the same way in char as they are in unsigned char, and hence that reinterpreting them is the same as converting them. If you had values greater then CHAR_MAX then implementations are allowed to treat them differently.

Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
  • What's the difference between the unsigned char's being converted to char and them being 'interpreted' as char? – Alex Bitek Apr 01 '13 at 12:08
  • @BadDesign: on most implementations it makes no difference, but on unusual architectures there could be one. The result of converting a value greater than `CHAR_MAX` to char is implementation-defined. The result of reinterpreting depends on the value representation of the types, and isn't guaranteed to be the same as converting. – Steve Jessop Apr 01 '13 at 23:10
  • Hi @steve Does the array need a trailing 0 before making a string out of it? – Noitidart Feb 22 '17 at 22:30
  • 1
    @Noitidart: no, the iterator constructor of `string` handles that. – Steve Jessop Mar 07 '17 at 09:40
7

Have you tried sstream?

     stringstream s;
     s << my_txt;

     string str_my_txt = s.str();
Robben_Ford_Fan_boy
  • 8,494
  • 11
  • 64
  • 85
  • It worked great for me, not sure why you say it does not work? – WilliamKF May 15 '10 at 16:43
  • 2
    Turns out I was just getting lucky, the missing terminating NULL causes ABR in some cases. See related question here: http://stackoverflow.com/questions/2889074/why-do-i-get-this-strange-output-behavior – WilliamKF May 22 '10 at 18:11
0
  int _tmain(int argc, _TCHAR* argv[])
  {
        unsigned char temp  = 200;
        char temp1[4];

        sprintf(temp1, "%d", temp);

        std::string strtemp(temp1);

        std::cout<<strtemp.c_str()<<std::endl;

        return 0;
   }
Praveer Kumar
  • 912
  • 1
  • 12
  • 25
  • 2
    While this code snippet may solve the question, including an explanation [really helps](//meta.stackexchange.com/q/114762) to improve the quality of your post. Remember that you are answering the question for readers in the future, not just the person asking now! Please [edit] your answer to add explanation, and give an indication of what limitations and assumptions apply. – Toby Speight Nov 11 '16 at 10:56