gpt4 book ai didi

c++ - 使用CPP获取dd图像的十六进制值

转载 作者:行者123 更新时间:2023-12-02 09:48:15 25 4
gpt4 key购买 nike

我想读取前4096个字节作为图像文件的十六进制(来自dd加密设备的luks)。因此,我首先使用unsigned long进行了测试,其值范围从0 to 18446744073709551615开始。但是用这个代码

int main() {
unsigned long c;

FILE *fp = fopen("C:\\image.dd", "r");
if (fp == NULL) {
fprintf(stderr, "Can't read file");
return 0;
}

while (!feof(fp)){ // while not end of file
c=fgetc(fp); // get a character/byte from the file
printf("%02x ",c); // and show it in hex format
}
fclose(fp);

return 0;
}
我将得到以下输出:
4c 55 4b 53 ba be 00 01 61 65 73 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 78 74 73 2d 70 6c 61 69 6e 36 34 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 73 68 61 32 35 36 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 40 31 ea 2e 93 28 55 cd 52 b6 c4 51 1e 0f b1 25 0e 2d 65 72 85 f2 41 97 b3 9b 76 ae 07 e5 53 ac 02 21 b4 ffffffff
我的 image.dd的前512个字节如下所示:
4C 55 4B 53 BA BE 00 01 61 65 73 00 00 00 00 00 
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 78 74 73 2D 70 6C 61 69
6E 36 34 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 73 68 61 32 35 36 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 10 00 00 00 00 40
31 EA 2E 93 28 55 CD 52 B6 C4 51 1E 0F B1 25 0E
2D 65 72 85 F2 41 97 B3 9B 76 AE 07 E5 53 AC 02
21 B4 1A 6F 0C 8D E2 08 62 91 4D 22 3D CA A2 51
19 0A 74 29 00 01 06 4B 32 38 38 33 34 64 34 66
2D 36 62 32 64 2D 34 37 33 62 2D 62 34 63 65 2D
33 31 38 32 36 65 64 61 65 39 63 39 00 00 00 00
00 AC 71 F3 00 10 64 B8 37 E9 07 F3 84 51 CF 51
23 E8 F2 8E 31 57 FE 2C DE D5 70 76 F2 1B B0 F8
95 33 A6 BB E4 4F 91 A8 00 00 00 08 00 00 0F A0
00 00 DE AD 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 02 00 00 00 0F A0
00 00 DE AD 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 03 F8 00 00 0F A0
00 00 DE AD 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 05 F0 00 00 0F A0
00 00 DE AD 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
00 00 00 00 00 00 00 00 00 00 07 E8 00 00 0F A0
00 00 DE AD 00 00 00 00 00 00 00 00 00 00 00 00
所以这很奇怪。为什么要这样做呢?现在为什么要用 unsigned long以及值 ffffffff呢?
编辑:我只是想像,那就是我是否使用 unsigned long或类似 unsigned int。但是问题是一样的。

最佳答案

您有四个错误。
首先,您在打印数据时不会检查读取是否成功。
检查应该在c=fgetc(fp);printf("%02x ",c);之间
其次,您将错误的类型传递给printf()并调用未定义的行为。
格式%02X需要unsigned int。 (根据this answer,在适当范围内的int也可以)
第三,以文本模式打开二进制文件。
因此,在0x1A(EOF)字节处停止读取。
要通过fopen()以二进制模式打开文件,应将b添加到模式字符串中。
第四,您不将读取限制为前4096个字节。
请注意, fgetc() 返回int,因此使用unsigned long是多余的。
固定代码:

#include <stdio.h>

int main() {
int i; // read size counter
int c; // use proper type

FILE *fp = fopen("C:\\image.dd", "rb"); // open file in binary mode
if (fp == NULL) {
fprintf(stderr, "Can't read file");
return 0;
}

for (i = 0; i < 4096; i++) { // while not end of size to read
c=fgetc(fp); // get a character/byte from the file
if (c == EOF) break; // stop at end of file
printf("%02x ",c); // and show it in hex format
}
fclose(fp);

return 0;
}

关于c++ - 使用CPP获取dd图像的十六进制值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63033355/

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