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

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

+4
source share
3 answers

Try strtol with base 2.

+11
source

Another possibility is value = std::bitset<8>("11111111").to_ulong() . This is more specialized for binary than strtol , so it can provide benefits if you want to manipulate some bits. For example, if you want to read a number, flip bit 5 and then convert.

+10
source

You say specifically 8 bits, therefore:

static_cast<char>(std::bitset<8>(str).to_ulong());

+4
source

Source: https://habr.com/ru/post/1344630/


All Articles