This has me pretty bothered as I should be able to do it but when I read in the hex number and assign it to an unsigned int when I print it out I get a different number. Any advice would be great. Thanks
#include <iostream>
#include <fstream>
#include <string>
using namespace std;
int main()
{
fstream myfile;
myfile.open("test.text");
unsigned int t开发者_StackOverflowester;
string test;
myfile >> hex >> tester;
cout << tester;
system("pause");
return 0;
}
I bet you don't get a "different number".
I bet you get the same value, but in decimal representation.
You're already extracting a hex-representation value (myfile >> hex >> tester
); now insert one, too (cout << hex << tester
)!
This works for hex value in string format to 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;
}
This works int to hex
#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;
}
Also check your filename. In my file It had a 54 written on it than output was 36 in hex.
精彩评论