Example
NOTE: that i am only concerned about letters. so bitset 000001 would be a
or A
.
I have a string
named s
with the value "abc"
.
I take each char
of the string
and convert it to binary value through
the use of bitset
.
e.g
bitset <6> b1 = s[0]; //a
bitset <6> b2 = s[1]; //b
bitset <6> b3 = s[2]; //c
then i want to put the results into an array
of strings
. The name of the array is arr
(and each string
of the array
will represent the binary value of each char
)
e.g
arr[0] //will hold the value of char 'a' in binary form which is 000001
arr[1] //will hold the value of char 'b' in binary form which is 000010
arr[2] //will hold the value of char 'c' in binary form which is 000011
and the way i convert each char
from the string
to binary is
arr[0] = b1.to_string(); //arr[0] is now 000001
arr[1] = b2.to_string(); //arr[1] is now 000010
arr[2] = b3.to_string(); //arr[2] is now 000011
Now here lies my problem.
How do i convert them back to char
?
e.g.
//I want each char to take back the each corresponding letter from the binary values
char c1; //How do i make the arr[0] value of 000001 to become 'a' again?
char c2; //Same here
char c3; //And here
Assuming you want to start at ASCII code 64, and that
'a'
(or'A'
) is simply000001
in that case, then you can simply do'A'
in decimal is65
, in binary is0b01000001
.'a'
in decimal is97
, in binary is0b01100001
. In your code, you use abitset<6>
to store'a'
(or'A'
). Abitset<6>
can only represent2^6
symbols, i.e.64
, so you will encounter cutting. Basically the2
most significant bits will be cut. In this case,bitset<6>('A')
becomes0b000001
, i.e.1
in decimal, andbitset<6>('a')
becomes0b1000001
, i.e.33
in decimal. You can now convince yourself that adding back64
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:consider the following:
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.
Since you stated you no longer need the
std::bitset
after you convert from binary back to yourchar
representation, you can avoid using it for the conversion.Interprets the original binary representation as a base 2 (binary) number and adds 64. Since you have the original
char
s stored in a binary format in thearr
array, you can pass them tostd::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 as0
), and the base of the string argument. Here's more info on that. The result of thestd::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 achar
.If you can afford to use a larger
std::bitset
you can even scrap adding 64.Here's a live demo:
Demo