二进制翻译

发布于 2024-11-02 05:13:25 字数 301 浏览 3 评论 0原文

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

葮薆情 2024-11-09 05:13:25

这应该以十六进制正确打印文件的第一个字节。

检查所使用的 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.

Spring初心 2024-11-09 05:13:25

抱歉,但不 - while (!feof(f)) 本质上总是错误的 - 它通常会读取文件中的最后一项两次。这是我几年前写的一个相当可用的六角转储器:

/* public domain by Jerry Coffin, tested with MS C 10.0 and BC 4.5
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    unsigned long offset = 0;
    FILE *input;
    int bytes, i, j;
    unsigned char buffer[16];
    char outbuffer[60];

    if ( argc < 2 ) {
        fprintf(stderr, "\nUsage: dump filename [filename...]");
        return EXIT_FAILURE;
    }

    for (j=1;j<argc; ++j) {

        if ( NULL ==(input=fopen(argv[j], "rb")))
            continue;

        printf("\n%s:\n", argv[j]);

        while (0 < (bytes=fread(buffer, 1, 16, input))) {
            sprintf(outbuffer, "%8.8lx: ", offset+=16);
            for (i=0;i<bytes;i++) {
                sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
                if (!isprint(buffer[i]))
                    buffer[i] = '.';
            }
            printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
        }
        fclose(input);
    }
    return 0;
}

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:

/* public domain by Jerry Coffin, tested with MS C 10.0 and BC 4.5
 */
#include <stdio.h>
#include <stdlib.h>

int main(int argc, char **argv) {
    unsigned long offset = 0;
    FILE *input;
    int bytes, i, j;
    unsigned char buffer[16];
    char outbuffer[60];

    if ( argc < 2 ) {
        fprintf(stderr, "\nUsage: dump filename [filename...]");
        return EXIT_FAILURE;
    }

    for (j=1;j<argc; ++j) {

        if ( NULL ==(input=fopen(argv[j], "rb")))
            continue;

        printf("\n%s:\n", argv[j]);

        while (0 < (bytes=fread(buffer, 1, 16, input))) {
            sprintf(outbuffer, "%8.8lx: ", offset+=16);
            for (i=0;i<bytes;i++) {
                sprintf(outbuffer+10+3*i, "%2.2X ",buffer[i]);
                if (!isprint(buffer[i]))
                    buffer[i] = '.';
            }
            printf("%-60s %*.*s\n", outbuffer, bytes, bytes, buffer);
        }
        fclose(input);
    }
    return 0;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文