2013-08-20 57 views
0

欲輸入的最多16個十六進制字符到屏幕上,然後一個字符串字符串轉換爲一個bitset < 64>轉換十六進制的64位位集C++

到目前爲止我已成功以下

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的方式不同?

謝謝。

+0

只是爲了確保,當你在做這64位,你是否將bitset <32>更改爲bitset <64> – DanChianucci

+0

有幾個問題: 您應該使用uint64_t作爲您的tempValue(只是爲了確保您具有合適的大小int)。 我假設你已經更新了addr以聲明bitset <64>而不是32. – dallasstar

+0

是的,我正在更新值,謝謝我應該這樣做! – user2646276

回答

1

有人猜測,你錯過了將某些東西改爲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 :abcdef 
addr = 0000000100100011010001010110011110001001101010111100110111101111 

[測試既VC++和MinGW,具有相同的結果]

+0

謝謝,我改變了很長的時間,並在我的代碼中將32位切換爲64位,並且仍然只有最低的32位工作。 – user2646276

+0

你能想出爲什麼字面上複製你的代碼不適合我嗎? ' 使用命名空間std 的#include 的#include 的#include ; int main() { string tempString; unsigned long long tempValue; cout <<「在十六進制中輸入地址:」; cin >> tempString; istringstream ost(tempString); ost >> hex >> tempValue; bitset <64> addr(tempValue); cout <<「addr =」<< addr << endl; } ' – user2646276