这让我很困扰,因为我应该能够做到这一点,但是当我读取十六进制数并将其分配给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;
}
我打赌你不会得到一个"不同的数字"。
我打赌你会得到相同的值,但是是十进制的。
您已经提取十六进制表示值(myfile >> hex >> tester
);现在也插入一个(cout << hex << tester
)!
这适用于十六进制字符串格式的值int
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
fstream myfile;
myfile.open("test.txt");
string fromFile;
unsigned int tester;
myfile >> fromFile;
istringstream iss(fromFile);
iss >> hex >> tester;
cout << tester;
system("pause");
return 0;
}
这适用于十六进制
#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