从文件中读取十六进制数字
Reading in HEX number from file
这让我很困扰,因为我应该能够做到这一点,但是当我读取十六进制数并将其分配给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
相关文章:
- 在十六进制中读取的数字在C 中给出了错误的数字
- 从char阵列中提取十六进制数字
- 如何在 c++ 中将十六进制数字转换为字符
- 使用字符串格式限制十六进制数字的数量
- 从输入中找到连续十六进制数字的最大数量
- 将十六进制数字从无符号字符转换为字符串
- 从 bin 文件 -c++ 中读取十六进制数字为每个字符 2 位数字
- 重载输出运算符给出十六进制数字而不是字符串
- 将十六进制数的 char[] 转换为对应于 ASCII 表中十六进制数字的字母的 char[] 并反转
- 从包含十六进制数字的std::string或QString创建std::bitset或QBitArray
- "first-chance exception..."消息中的十六进制数字是什么意思?
- 一次打印 2 个字节的十六进制数字
- 连接两个十六进制数字
- 分析64位十六进制数字
- 在一行代码中输入2个十六进制数字和一个字符串
- 十六进制数字系统中的指针值
- 将十六进制文件读取到二进制缓冲区的文件流
- 将std::字符串数字转换为十六进制数字
- 格式化带符号十六进制数字的输出
- 从文件中读取十六进制数字