简体   繁体   English

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

[英]converting hex to 64 bit bitset c++

I want to input a string of up to 16 hex characters to the screen and then convert the string to a bitset<64> 我想在屏幕上输入最多16个十六进制字符的字符串,然后将字符串转换为bitset <64>

So far I have managed the following 到目前为止,我已经管理了以下内容

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;

which works fine, but then when I repeat for 64 bit it fails. 效果很好,但是当我重复64位操作时,它失败了。 Playing around it seems to only fail for the top 32 bits! 玩它似乎只对前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;
}

Is this to do with a maximum size for the istringstream? 这与istringstream的最大大小有关吗? Or perhaps the different way I am assigning wdata? 还是我分配wdata的方式不同?

Thanks. 谢谢。

At a guess, you missed changing something to 64 bits (either the bitset, or possibly changing the int to long long ). 猜测中,您错过了将某些内容更改为64位(该位集,或者可能将int更改为long long )的想法。 This, however: 但是,这是:

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;

...seems to work, at least for me: ...似乎可以工作,至少对我来说:

enter addr in hex : 0123456789abcdef
addr = 0000000100100011010001010110011110001001101010111100110111101111

[tested with both VC++ and MinGW, with identical results] [使用VC ++和MinGW进行测试,结果相同]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM