0

我想在屏幕上输入最多 16 个十六进制字符的字符串,然后将该字符串转换为 bitset<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;
}

这与 istringstream 的最大大小有关吗?或者也许是我分配 wdata 的不同方式?

谢谢。

4

1 回答 1

1

猜测一下,您错过了将某些内容更改为 64 位(bitset 或可能更改intlong 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 测试,结果相同]

于 2013-08-20T17:47:43.433 回答