从C ++中的txt文件读取十六进制 [英] Reading hexadecimals from a txt file in C++
问题描述
我有一个txt文件,其中包含十六进制形式的每行中的整数,如:
232B2344
A2BC34893
DEF9433
.....
我想在C ++中读取这个.txt文件并将它们存储在数组中,以便我可以对它们进行计算的一种优雅的方式。还给了hex 232B2344说我想在位置说每个字节条目的值
I have a txt file which contains integers in hexadecimal form in each line like: 232B2344 A2BC34893 DEF9433 ..... I would like an elegant way of reading this .txt file in C++ and store them in arrays so that I can make computations on them. Also given the hex 232B2344 say I would like to be in position to say the value of each byte entry
推荐答案
首先包括头
#include "sstream"
然后使用文件处理在一个简单的char []数组字符串中获取十六进制值。
Then get the hexadecimal value in a simple char[] array string using file handling.
char str [100];
char str[100];
声明一个Unsigned int变量...
Declare an Unsigned int variable...
unsigned int value;
然后声明一个std :: stringstream变量。例如
Then declare a "std::stringstream" variable. e.g.
std::stringstream ss;
ss<<std::hex<<str;
ss>>value;
现在,value以整数形式包含文件中的十六进制值。
Now the "value" contains the hexadecimal value in file in form of integer.
这篇关于从C ++中的txt文件读取十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!