How to convert string of binary values back to char

家住魔仙堡 提交于 2019-12-04 02:39:10

Assuming you want to start at ASCII code 64, and that 'a' (or 'A') is simply 000001 in that case, then you can simply do

c1 = static_cast<char>(std::bitset<6>(arr[0]).to_ulong() + 64); // 

'A' in decimal is 65, in binary is 0b01000001. 'a' in decimal is 97, in binary is 0b01100001. In your code, you use a bitset<6> to store 'a' (or 'A'). A bitset<6> can only represent 2^6 symbols, i.e. 64, so you will encounter cutting. Basically the 2 most significant bits will be cut. In this case, bitset<6>('A') becomes 0b000001, i.e. 1 in decimal, and bitset<6>('a') becomes 0b1000001, i.e. 33 in decimal. You can now convince yourself that adding back 64 produces the right result.

EDIT

Note that you can also use std::stoi (C++11 only) to convert the bit string from base 2 to decimal, as mentioned in the other answers:

char c1 = static_cast<char>(std::stoi(arr[0], nullptr, 2) + 64);

Since you stated you no longer need the std::bitset after you convert from binary back to your char representation, you can avoid using it for the conversion.

static_cast<char>(std::stoi(arr[i],0,2) + 64);

Interprets the original binary representation as a base 2 (binary) number and adds 64. Since you have the original chars stored in a binary format in the arr array, you can pass them to std::stoi and specify that the values are base 2, in the 3rd parameter. std::stoi requires 3 parameters: the string you're trying to convert, a pointer to an int that will store the index of the first unconverted character (unneeded here,so can be left as 0), and the base of the string argument. Here's more info on that. The result of the std::stoi call is the base 10 (decimal) equivalent of the binary values. vsoftco's answer explains why adding 64 is the appropriate operation to do here after getting a decimal representation. The result of this is given back as a char.

If you can afford to use a larger std::bitset you can even scrap adding 64.

Here's a live demo:

Demo

consider the following:

 std::cout << "abc" << std::endl;

 std::cout << 'a' << 'b' << 'c' << std::endl;

 std::cout << std::dec
           << static_cast<int>('a') << " "
           << static_cast<int>('b') << " "
           << static_cast<int>('c')  << " "<< std::endl;

 std::cout << std::hex
           << static_cast<int>('a') << " "
           << static_cast<int>('b') << " "
           << static_cast<int>('c')  << " "<< std::endl;

with output

abc

abc

97 98 99

61 62 63

This shows that each char is binary, and 97 dec is 0x61 hex.

Conversion (to / from binary via bitset) is not necessary.

(or perhaps I am not understanding why you want to do nothing in a somewhat complicated fashion).

Note that static_cast<> causes no code gen. Note that std::dec and std::hex do no change to the data, just to the radix.

edit --- For only 8 bits, you might consider this ... no endian issues.

 std::cout << ((('a' >> 7) & 1) ? '1' : '0')
           << ((('a' >> 6) & 1) ? '1' : '0')
           << ((('a' >> 5) & 1) ? '1' : '0')
           << ((('a' >> 4) & 1) ? '1' : '0')
           << ((('a' >> 3) & 1) ? '1' : '0')
           << ((('a' >> 2) & 1) ? '1' : '0')
           << ((('a' >> 1) & 1) ? '1' : '0')
           << ((('a' >> 0) & 1) ? '1' : '0') << "  "
           << ((('b' >> 7) & 1) ? '1' : '0')
           << ((('b' >> 6) & 1) ? '1' : '0')
           << ((('b' >> 5) & 1) ? '1' : '0')
           << ((('b' >> 4) & 1) ? '1' : '0')
           << ((('b' >> 3) & 1) ? '1' : '0')
           << ((('b' >> 2) & 1) ? '1' : '0')
           << ((('b' >> 1) & 1) ? '1' : '0')
           << ((('b' >> 0) & 1) ? '1' : '0') << "  "
           << ((('c' >> 7) & 1) ? '1' : '0')
           << ((('c' >> 6) & 1) ? '1' : '0')
           << ((('c' >> 5) & 1) ? '1' : '0')
           << ((('c' >> 4) & 1) ? '1' : '0')
           << ((('c' >> 3) & 1) ? '1' : '0')
           << ((('c' >> 2) & 1) ? '1' : '0')
           << ((('c' >> 1) & 1) ? '1' : '0')
           << ((('c' >> 0) & 1) ? '1' : '0') << "  "
           << std::endl;


 std::cout << std::dec << std::endl;


 // with variable
 char zulu = 'A';

 std::cout << std::dec
           << "NOTE: in this cout, every use of zulu is a 'read' \n"
           << "   zulu: " << zulu                               << "  \n"

           << "   dec : " << std::dec << static_cast<int>(zulu) << "  \n"
           << "   --- : " << zulu                               << "  \n" // zulu not changed

           << "   hex : " << std::hex << static_cast<int>(zulu) << "  \n"
           << "   --- : " << zulu                               << "  \n" // zulu not changed

           << "   bin : "
           << (((zulu >> 7) & 1) ? '1' : '0')
           << (((zulu >> 6) & 1) ? '1' : '0')
           << (((zulu >> 5) & 1) ? '1' : '0')
           << (((zulu >> 4) & 1) ? '1' : '0')
           << (((zulu >> 3) & 1) ? '1' : '0')
           << (((zulu >> 2) & 1) ? '1' : '0')
           << (((zulu >> 1) & 1) ? '1' : '0')
           << (((zulu >> 0) & 1) ? '1' : '0')    << "  \n"
           << "   --- : " << zulu                               << "  \n" // zulu not changed

           << " bitset: " << std::bitset<8>(zulu)               << "  \n"
           << "   zulu: " << zulu                               << "  \n\nzulu not changed!" // zulu not changed

           << std::endl;
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!