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); 
5
  • 1
    And why don't you like reinterpret_cast...? Commented May 15, 2010 at 16:40
  • 3
    It always seems like a hack to force a cast, I'd rather use things as is rather than force the complier to treat them as something else. Commented May 15, 2010 at 17:43
  • Why don't your use char for my_txt; after all, those values you posted are ASCii. This may lead to solving other issues. Commented May 15, 2010 at 19:04
  • 1
    It is a generated file so I'd rather not have to further process it. Commented May 22, 2010 at 18:01
  • Initializing a string from unsigned char* seems pretty reasonable to me. I'm surprised anyone has an objection to wanting to do that without using a cast. Commented Feb 19, 2015 at 0:02

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

4 Comments

What's the difference between the unsigned char's being converted to char and them being 'interpreted' as char?
@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.
Hi @steve Does the array need a trailing 0 before making a string out of it?
@Noitidart: no, the iterator constructor of string handles that.
7

Have you tried sstream?

 stringstream s; s << my_txt; string str_my_txt = s.str(); 

2 Comments

It worked great for me, not sure why you say it does not work?
Turns out I was just getting lucky, the missing terminating NULL causes ABR in some cases. See related question here: stackoverflow.com/questions/2889074/…
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; } 

1 Comment

While this code snippet may solve the question, including an explanation really helps 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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.