string convert_binary_to_hex(string binary_value, int number_of_bits)
{
bitset<number_of_bits> set(binary_value);
ostringstream result;
result << hex << set.to_ulong() << endl;
return result.str();
}
在上述方法中,我将二进制字符串转换为十六进制字符串。由于十六进制值是4位的,因此
number_of_bits
变量需要是4的倍数,因为binary_value
在我编写的应用程序中可以从4位到256位不等。如何使bitset接受可变大小?
我的导入:
#include <stdio.h>
#include <iostream>
#include <string>
#include <bitset>
#include <sstream>