11

I have bitset<8> v8 and its value is something like "11001101", how can I convert it to char? I need a single letter. Like letter "f"=01100110.

P.S. Thanks for help. I needed this to illustrate random errors in bits. For example without error f, and with error something like ♥, and so on with all text in file. In text you can see such errors clearly.

Van
  • 113
  • 1
  • 1
  • 5

2 Answers2

12
unsigned long i = mybits.to_ulong(); 
unsigned char c = static_cast<unsigned char>( i ); // simplest -- no checks for 8 bit bitsets

Something along the lines of the above should work. Note that the bit field may contain a value that cannot be represented using a plain char (it is implementation defined whether it is signed or not) -- so you should always check before casting.

char c;
if (i <= CHAR_MAX) 
c = static_cast<char>( i );
Steve Jessop
  • 273,490
  • 39
  • 460
  • 699
dirkgently
  • 108,024
  • 16
  • 131
  • 187
  • 1
    Although if it's a `bitset<8>`, it's not going to contain a value that can't be represented in `unsigned char`. So that might be a better option than `char`. And you *could* just throw yourself on the mercy of your implementation's conversion of out-of-range values to signed types. – Steve Jessop Jun 17 '12 at 01:50
  • We don't know if plain `char` is signed. I assumed `8` to be an example. But I'll fix that up. – dirkgently Jun 17 '12 at 01:50
  • 2
    Exactly, that's why I say `unsigned char` might be a better option, although only "might" because if the questioner really needs a `char`, then what you said first is correct, they should check the value. And why assume `8` to be an example, it's not only stated twice in the question, it's a natural size of bitset to want to convert to `char`. "How do I convert a `bitset<127>` to char" would have been a much stupider question... – Steve Jessop Jun 17 '12 at 01:52
  • @SteveJessop: Yup. That was one reason for the check. – dirkgently Jun 17 '12 at 01:56
  • I don't know why but I was thinking that the real problem is about reading in UTF-8 bytes and storing them in a `bitset` and the question wasn't giving us the whole story. Perhaps, I was thinking too much. – dirkgently Jun 17 '12 at 01:59
  • 1
    I didn't even try to think of a reason for using `bitset<8>` in preference to `unsigned char` or `uint8_t`. I'm sure there are some :-) – Steve Jessop Jun 17 '12 at 02:02
  • I too avoid `bitset` like the plague. Which is perhaps why my caffeine-deficient mind was trying to retrofit the usage. – dirkgently Jun 17 '12 at 02:05
  • Using `bitset` makes sense to me when the size of the bitset is something like `MAX_FLAG+1`, where `MAX_FLAG` has to do with some enum containing the indexes of a set of flags. Using it for numbers seems... odd. So I don't avoid it like the plague, maybe more like a bad sniffle. – Steve Jessop Jun 17 '12 at 02:11
  • Ah! The old bitwise OR thingammy for configuration! FWIW, this question could well be homework (since there was a similar question a year back). – dirkgently Jun 17 '12 at 02:12
  • If you are happy to get whatever value your platform assigns to chars with the highest bit set, then you can convert in two steps to get a char which has exactly the same bits set as the bitset via `char c = static_cast(static_cast(bs.to_ulong()));`. The truncation from `unsigned long` to `unsigned char` is always well-defined by the standard, and the conversion from `unsigned char` to `char` is implementation-defined. – Kerrek SB Jun 17 '12 at 10:51
0

The provided solution did not work for me. I was using C++14 with g++ 9. However, I was able to get it working by :

char lower = 'a';
bitset<8> upper(lower);
upper.reset(5);
cout << (char)upper.to_ulong() << endl;

This may not be the best way to do it, I am sure, but it worked for me!

Lee Taylor
  • 7,761
  • 16
  • 33
  • 49
Ravi Tiwari
  • 946
  • 8
  • 17