将十六进制转换为64位的位集c++

converting hex to 64 bit bitset c++

本文关键字:c++ 64位 十六进制 转换      更新时间:2023-10-16

我想在屏幕上输入一个最多16个十六进制字符的字符串,然后将该字符串转换为位集<64>

到目前为止,我已经管理了以下

string tempString;
unsigned int tempValue;
cout << "enter addr in hex : ";
cin >> tempString;
istringstream ost(tempString);
ost >> hex >> tempValue;
bitset<32>  addr(tempValue);
cout << "addr = " << addr << endl;

它工作得很好,但当我重复64位时,它失败了。绕过它似乎只会在前32位失败!

bitset<64> wdata = 0;
if (rdnwr[0] == 0)
{
    cout << "enter wdata in hex : ";
    cin >> tempString;
    istringstream ost1(tempString);
    ost1 >> hex >> tempValue;
    wdata = tempValue;
    cout << "wdata = " << wdata << endl;
}

这与分发流的最大大小有关吗?或者我分配wdata的不同方式?

谢谢。

猜测,您错过了将某些内容更改为64位(位集,或者可能将int更改为long long)。然而:

string tempString;
unsigned long long tempValue;
cout << "enter addr in hex : ";
cin >> tempString;
istringstream ost(tempString);
ost >> hex >> tempValue;
bitset<64>  addr(tempValue);
cout << "addr = " << addr << endl;

似乎有效,至少对我来说是这样:

enter addr in hex : 0123456789abcdef
addr = 0000000100100011010001010110011110001001101010111100110111101111

【用VC++和MinGW测试,结果相同】