所以我有一个字符串 x = "10101",我需要将 x 中二进制的十六进制值放入任何字符串 y 中。所以如果 x="10101"
那么 y="0x15"
最佳答案
最简单的方法是使用 [bitset][1]:
#include <iostream>
#include <string>
#include <bitset>
using namespace std;
int main(){
string binary_str("11001111");
bitset<8> set(binary_str);
cout << hex << set.to_ulong() << endl;
}
但我读到这不是最有效的方法...取决于您的需求。请记住,过早的优化是万恶之源。
关于c++ - 将表示二进制的字符串转换为表示等效十六进制的字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5628815/