Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++: I have a string representing 8 bits. How Do I convert it to a char?

Tags:

c++

ie. "11111111" should convert to 0b11111111 / 255 (in dec)

like image 574
NullVoxPopuli Avatar asked Nov 28 '22 03:11

NullVoxPopuli


2 Answers

Try strtol with a base of 2.

like image 174
nobody Avatar answered Dec 05 '22 10:12

nobody


Another possibility would be value = std::bitset<8>("11111111").to_ulong(). This is more specialized for binary than strtol, so it could provide advantages if you might want to manipulate some bits. E.g., if you wanted to read a number, flip bit 5, and then convert.

like image 30
Jerry Coffin Avatar answered Dec 05 '22 10:12

Jerry Coffin