gpt4 book ai didi

c - printf 添加额外的 `FFFFFF` 到 char 数组的十六进制打印

转载 作者:太空狗 更新时间:2023-10-29 16:43:25 28 4
gpt4 key购买 nike

<分区>

考虑下面的简化代码。我想从文件中提取一些二进制数据/流并将其以十六进制格式打印到标准输出。

我得到了额外的 3 个字节 0xFFFFFF。怎么了?额外的字节从哪里来?

输出

in:
2000FFFFFFAF00690033005A00
out:
2000FFFFFFAF00690033005A00

program.c

#include <stdio.h>
#include <stdlib.h>

int main(int argc, char** argv) {

int i;
char raw[10] = {0x20,0x00,0xAF,0x00,0x69,0x00,0x33,0x00,0x5A,0x00};
FILE *outfile;
char *buf;

printf("in:\n\t");
for( i=0; i<10; i++ )
printf("%02X", raw[i]);

outfile = fopen("raw_data.bin", "w+b");

fwrite(raw, 1, 10, outfile);

buf = (char *) malloc (32 * sizeof(char));
fseek(outfile, 0, SEEK_SET);
fread(buf, 1, 10, outfile);

printf("\nout:\n\t");
for( i=0; i<10; i++ )
printf("%02X", buf[i]);

printf("\n");

fclose(outfile);
return 0;
}

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com