【发布时间】:2015-05-01 11:37:20
【问题描述】:
我已经为此苦苦挣扎了一段时间。我正在尝试读取其中包含十六进制信息的文件,假设文件的内容如下所示。
00 00 e0 3a 12 16 00 ff fe 98 c4 cc ce 14 0e 0a aa cf
我正在寻找一种在 C++ 中读取每个“字节”信息(同时忽略空格)的方法。
由于我正在编写的代码是类的一部分,因此我创建了一个小程序来演示我目前正在做的事情。
int main(void)
{
int i = 0;
unsigned char byte;
unsigned char Memory[50];
ifstream f_in;
f_in.open("file_with_hex_stuff.txt");
f_in >> skipws; // Skip Whitespace
while(f_in >> hex >> byte)
{
Memory[i++] = byte;
}
return 0;
}
(抱歉,如果上面的代码无法编译,这只是为了让您了解我想要的东西。 我希望数组是这样的:
Memory[0] => 0x00;
Memory[1] => 0x00;
Memory[2] => 0xe0;
Memory[3] => 0x3a;
Memory[4] => 0x12;
等等……
但它会将每个数字加载到它自己在数组中的位置,如下所示:
Memory[0] => 0x00; // 2 LOCATIONS USED FOR 0x00
Memory[1] => 0x00; //
Memory[2] => 0x00; // 2 LOCATIONS USED FOR 0x00
Memory[3] => 0x00; //
Memory[4] => 0x0e; // 2 LOCATIONS USED FOR 0xe0
Memory[5] => 0x00; //
Memory[6] => 0x03; // 2 LOCATIONS USED FOR 0x3a
Memory[7] => 0x0a; //
等等……
如果这篇文章没有意义,请采纳。任何反馈表示赞赏。
谢谢。
【问题讨论】: