First, I use Python's read method to read a file,and print binary data of the file. The python code is :
aaa = open('D:\\CbProject\\testphoto2.jpg','rb')
a = aaa.read()
print(a) #十六进制显示 二进制byte类型数据
print(len(a)) #39038
Python can display binary data of files in hexadecimal.Data type is [bytes].
The python result is :
This seems normal,as you can see, the binary form of the file is “\xff \xd8 \xff \xe0 ......” Next, I use the C language's FREAD and read methods to read the file and print its binary form.
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MYSIZE 39038
int main(void)
{
FILE *fp;
if((fp = fopen("D:\\CbProject\\testphoto2.jpg", "rb")) == NULL) {
fprintf(stderr, "can't open file\n");
exit(1);
}
char buffer[MYSIZE];
if(fread(buffer, sizeof(buffer), 1, fp) != 1) {
fprintf(stderr, "read failed");
exit(1);
}
for (int i = 0; i < sizeof(buffer); i++) {
printf("%2x ", buffer[i]);
}
fclose(fp);
return(0);
}
why? I have a lot of questions.First of all, the char of C language can only store one byte, but the display of many data is obviously larger than one byte.One byte equals 0xff.Why are there so many f's here. %2x ,Why can I print a 1, a 2, a 3...?