简体   繁体   English

以十六进制读取文件给出错误的输出(跳过一些十六进制值)

[英]Reading file in hex gives wrong output (skips some hex values)

I'm trying to read/print out the hex instructions from the Chip8 version of Pong (which can be downloaded here ).我正在尝试从 Chip8 版本的 Pong(可以在此处下载)读取/打印十六进制指令。
Here is the code I am using to read in ROM:这是我用来读取 ROM 的代码:

//Open ROM
string dir = "Test/PONG";
ifstream inf(dir.c_str(), ios::binary);

unsigned char input;
for(int i = 0; inf >> input; i++) {
    //Each loop prints 1 bytes
    //New line every 16 bytes and print line number
    if(i%16 == 0) {
        cout << endl;
        cout << setw(7) << setfill('0') << i << " ";
    }
    //2 extra spaces after 8 bytes
    else if(i%8 == 0)
        cout << "  ";
    cout << hex << setw(2) << setfill('0') << (int)input << " ";
}
cout << endl;

And here's part of the output:这是输出的一部分:

0000000 6a 02 6b 6c 3f 6d a2 ea   da b6 dc d6 6e 00 22 d4  
0000010 66 03 68 02 60 60 f0 15   f0 07 30 00 12 1a c7 17  
0000020 77 08 69 ff a2 f0 d6 71   a2 ea da b6 dc d6 60 01  

... ...

And here's the output that I believe is correct which I got from running the command "hexdump -C PONG" in the terminal:这是我认为正确的输出,这是我在终端中运行命令“hexdump -C PONG”得到的:

00000000  6a 02 6b 0c 6c 3f 6d 0c  a2 ea da b6 dc d6 6e 00  
00000010  22 d4 66 03 68 02 60 60  f0 15 f0 07 30 00 12 1a  
00000020  c7 17 77 08 69 ff a2 f0  d6 71 a2 ea da b6 dc d6  

... ...

On the first line of hex values, you can see the value '0c' twice in terminal but the output of the code skips printing '0c'.在十六进制值的第一行,您可以在终端中看到两次值“0c”,但代码输出跳过打印“0c”。 I'm not sure why my code is skipping '0c'.我不确定为什么我的代码跳过“0c”。 Am I reading the file improperly?我是否错误地读取了文件? Is the command "hexdump" providing the wrong output?命令“hexdump”是否提供了错误的输出? What can I change about the way I read the file?我可以改变阅读文件的方式吗?

EDIT: Using inf.get(input) is able to read in '0c' however it also outputs some hex values differently.编辑:使用 inf.get(input) 能够读取 '0c' 但是它也会以不同的方式输出一些十六进制值。
For example:例如:

6a 02 6b 0c 6c 3f 6d 0c ffffffa2 ffffffea ffffffda ffffffb6 ffffffdc ffffffd6 6e 00 22 ffffffd4 66 03 68 02 60 60 fffffff0

EDIT 2: The 'f's are printed since some hex values are read as negative numbers.编辑 2: 'f's 被打印,因为一些十六进制值被读取为负数。 So I made an if statement that checks if input is negative, if it is negative I multiply it by -1 then print the hex value. 所以我做了一个 if 语句来检查输入是否为负数,如果是负数,我将它乘以 -1 然后打印十六进制值。

EDIT 3: I decided to use a stringstream to only print out the last 2 characters for negative hex values:编辑 3:我决定使用 stringstream 只打印负十六进制值的最后 2 个字符:

stringstream convert;
if((int)input < 0) {
     convert << hex << (int)input;
     cout << convert.str().substr(6,8) << " ";
     convert.str("");
}

EDIT 4: The hex '0c' represents the escape sequence '\\f' and is therefore ignored when printing.编辑 4:十六进制 '0c' 表示转义序列 '\\f',因此在打印时被忽略。 Using inf.get(input) instead of inf >> input stores characters into input as unformatted characters (I think).使用inf.get(input)而不是inf >> input将字符作为未格式化的字符存储到输入中(我认为)。 However the get() function can't take a unsigned char as an argument.但是get()函数不能将unsigned char作为参数。 So I have to cast input to char& .所以我必须将输入转换为char&
In conclusion, I had to change inf >> input to inf.get((char&)input) .总之,我不得不将inf >> input更改为inf.get((char&)input)

Source: http://www.cplusplus.com/reference/istream/istream/get/来源: http : //www.cplusplus.com/reference/istream/istream/get/

Operator>> reads formatted data (char in your case) from the input stream, so a char with value 12 (0c in hex) is considered as ASCII form feed and ignored ( as a value of 32 or 0x20 would be considered a space ). Operator>> 从输入流中读取格式化数据(在您的情况下为 char),因此值为 12(十六进制为 0c)的 char 被视为 ASCII 换页并被忽略(因为 32 或 0x20 的值将被视为空格) . Char are also signed values and an hex value of a2 (which is decimal 162) would be stored as a negative value. Char 也是有符号值,a2 的十六进制值(十进制 162)将存储为负值。 Promoting that value to an int would keep the sign and due to complementation, when printed as unsigned you'll get the ffff's.将该值提升为 int 将保留符号,并且由于互补,当打印为无符号时,您将获得 ffff。

keep the保持

unsigned char input;

then to read the values use然后读取值使用

inf.get(static_cast<char>(input))

and to print并打印

cout << hex << setw(2) << setfill('0') << static_cast<unsigned int>(input) << " ";

You are using (signed) int as your variable.您正在使用(signed) int作为变量。 When a signed int is negative and printed in hex, it will be preceded by one or more F digits.当一个有signed int为负数并以十六进制打印时,它前面将有一个或多个F数字。

I highly recommend using unsigned int for your values.我强烈建议您使用unsigned int作为您的值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM