2011-07-21 51 views
0

這讓我非常煩惱,因爲我應該可以做到這一點,但是當我讀取十六進制數並在打印出來時將它分配給unsigned int時,我得到了不同的數字。任何建議都會很棒。謝謝從文件中讀取十六進制數

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() 
{ 
fstream myfile; 
myfile.open("test.text"); 
unsigned int tester; 
string test; 
myfile >> hex >> tester; 
cout << tester; 
system("pause"); 
return 0; 
} 
+0

有何不同?像不再是六角?請問'cout << hex << tester;'help? –

+3

我從來不理解在評論中寫回答的人。 –

+0

文件中有什麼? – shobhonk

回答

1

我敢打賭,你沒有得到「不同的號碼」。

我敢打賭,你會得到相同的價值,但在十進制表示。

您已經提取了十六進制表示值(myfile >> hex >> tester);現在也插入一個(cout << hex << tester)!

+0

我以前做過,現在就試一試。我在屏幕上得到的是cccccccc。我的編號是0041f7a0在我的文件中,在調試過程中變量有3435973836。 – Intelwalk

+2

cccccccc在調試模式下是未初始化的值。您不檢查打開的文件是否成功。也許你實際上沒有從文件中讀取值? –

+0

(有時,根據您的實施和運行時間等) –

0

這適用於十六進制值的字符串格式爲int

​​

這工作INT詛咒

#include <iostream> 
#include <fstream> 
#include <string> 
using namespace std; 

int main() 
{ 
fstream myfile; 
myfile.open("test.txt"); 
unsigned int tester; 
string test; 
myfile >> tester; 
cout << hex << tester; 

system("pause"); 
return 0; 
} 

還要檢查你的文件名。在我的文件中它有一個54寫在它上面,而輸出是十六進制的36。

+0

他正在讀「十六進制數」。您的代碼以十進制解析。 –

+0

修復了代碼工作的第一位 – shobhonk

+0

「int to hex」是沒有意義的。 「int」是一個數字值,不管您決定打印它的人類可讀表示。 [編輯:好的,你也說[字符串]。]而你仍然*不讀取十六進制表示的數字,並將其打印爲十六進制表示。 –

相關問題