将二进制数据转化为字符显示,使用sprintf函数。
%08x以16进制方式输出的格式字符串。
08的含义为,输出的16进制值占8位,不足部分左侧补0。
unsigned int data;
int len;
FILE *fp1 = fopen(1.bin, "rb");//read only
if(fp1 == NULL)
{
printf("bin file open fail\n");
return -1;
}
FILE *fp2 = fopen(2.txt, "w+");//r w create
if(fp2 == NULL)
{
printf("txt file open fail\n");
return -1;
}
while(fread(&data, 1, 4, fp1))
{
len = sprintf(buffer, "%08x\n", data);
fwrite(buffer, 1, len, fp2);
}