How could I convert a std::string to an unsigned char? | SoloLearn: Learn to code for FREE!

0

How could I convert a std::string to an unsigned char?

Assuming the string has the value “0xFF”, how could I convert this to a single unsigned char (Uint8)? I have attempted: Uint8 *x = (unsigned char*)x->c_str(); But of course this returns an unsigned char array, rather than the single char. I have also tried: (Uint8)x->c_str(); But I receive the error “loses precision” when converting from const char* to unsigned char.

c++

7/8/2020 12:58:01 AM

NULL

16 Answers

New Answer

+6

NULL here is a link from stackoverflow which might help you understand how to convert with examples https://stackoverflow.com/a/51856080/4371981

+6

You can do it like in this code. But it is really an overhead, but you asked for it https://code.sololearn.com/ch2kS10le7nl/?ref=app

+4

NULL That is what i have done. In the code you can use uint8_t instead of unsigned char (your question says so hence i used it) for uint8_t x = 0xFF; have you tried printing the value of 'x'? Try it here, you will get an equivalent of char 255 (some box with '?' symbol) why because 0xFF will be converted to 255 internally.

+4

NULL I don't think you have understood my code at all. It is doing what you want. And you are not understanding one thing that if you assign 0xFF to a variable of type uint8_t or unsigned char or signed char it will be converted to int, you can only see it as 0xFF in memory view. If you try to print it to the console you will get char equivalent of ascii value 255

+4

NULL If my previous response was confirmed - that you wanted to convert std::string("0xff") into an 8bit value (0xff) to be stored in an uint8_t later on. Then I agree with ~ swim ~ approach. It is also most compatible (use of std::stringstream). I could suggest you to use std::stoi() for the same goal, but IIRC it requires a C++11 compliant compiler : )

+4

~ swim ~ If he (NULL) really wants to see 0xFF rather than 255 then maybe you can use this line cout << hex << showbase << uppercase << (int)ucval << '\n'; To print the output😀

+4

Also ~ swim ~ I think it would be better if you had used an istringstream rather than a stringstream as it is more efficient. istringstream ss{str}; int val;ss>>hex>>val; But anyway, great code 👍👍

+3

Are you trying to have the std::string("0xff") to be converted to unsigned char with value 255 (0xff)? actually I didn't get what you are trying to do ...

+3

Anthony Maina You are right istringstream would have been more efficient. Thanks for pointing that out. 🙂🙏

+2

Anthony Maina He wants to convert to 0xFF. It doesn't make sense to take that much of trouble to convert "0xFF" to see the value again as 0xFF in the display. A string value will be displayed as 0xFF and so an unsigned char after conversion. No body can say without seeing the code that the diplayed value came from a string or an unsigned char. He is probably not understanding that the value cannot stay in the hex form after conversion to unsigned char.

+1

~ swim ~ Ipang uint8_t x = 0xFF is legal since it is a hex value. uint8_t x = “0xFF” is not as it is a string. The goal is to convert std::string “0xFF” to uint8_t 0xFF.

+1

~ swim ~ uint8_t x = 0xFF works fine, it’s the conversion from std::string x = “0xFF” to uint8_t x = 0xFF that I can’t seem to find anywhere.

+1

Ipang I have updated my code for stoi also.

+1

Ipang I have updated my code for stoi also.

+1

~ swim ~ That was exactly what I had in mind 👍

0

BroFar Every stackoverflow answer I’ve found, including this one, gives an answer as to how I would store the byte in a char array, as opposed to a single char.