二进制翻译
#include <stdio.h>
int main() {
unsigned char data[1];
FILE *f = fopen("bill.jpg", "rb");
while (!feof(f)) {
if (fread(data, 1, 1, f) > 0) {
printf("0x%02x\n", data[0]);
}
}
fclose(f);
}
这是正确的吗?我很担心,因为如果我使用 hexdump 查看文件,我会得到完全不同的输出。
#include <stdio.h>
int main() {
unsigned char data[1];
FILE *f = fopen("bill.jpg", "rb");
while (!feof(f)) {
if (fread(data, 1, 1, f) > 0) {
printf("0x%02x\n", data[0]);
}
}
fclose(f);
}
Is this the right? I am worried because if I view the file using hexdump, I get completely different output.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
这应该以十六进制正确打印文件的第一个字节。
检查所使用的 Hexdump 实用程序的文档,或告诉我们正在使用哪个平台。一些转储实用程序在每行上以相反的顺序显示字节,以使小端读取更加直观 - 一旦您了解了它在做什么。
That should correctly print the first byte of the file in hex.
Check the documentation for the Hexdump utility used, or tell us which platform is being used. Some dump utilities display the bytes in reverse order on each line to make little-endian reading somewhat more intuitive—once you understand what it is doing.
抱歉,但不 -
while (!feof(f))
本质上总是错误的 - 它通常会读取文件中的最后一项两次。这是我几年前写的一个相当可用的六角转储器:I'm sorry, but no --
while (!feof(f))
is essentially always wrong -- it'll typically appear to read the last item in the file twice. Here's a reasonably usable hex dumper I wrote a few years ago: