例子
注意:我只关心字母。所以bitset 000001会是a
或A
。
我有一个名为s
的string
,值为"abc"
。
我取出string
中的每个char
,并通过使用bitset
将其转换为二进制值。
例如
bitset <6> b1 = s[0]; //a
bitset <6> b2 = s[1]; //b
bitset <6> b3 = s[2]; //c
我希望将结果放入一个字符串数组中,数组的名称为arr
(每个string
代表一个char
的二进制值)。
例如:
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
我将字符串中的每个char
转换为二进制的方法是:
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
现在我的问题在于,如何将它们转换回char
?
例如:
//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
char
,为什么不直接删除bitset
中间人并执行类似于static_cast<char>(std::stoi(arr[i]) + 64)
的操作呢?但是,我不知道您是否需要在代码中稍后进行进一步操作。 - Alejandro