📄 bin2txt.c
字号:
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <sys\stat.h>
#define RWMAXBUFLEN 17
int main(int argc, char *argv[])
{
unsigned char wbuf[RWMAXBUFLEN] = {0}, rbuf[RWMAXBUFLEN] = {0};
short int ch;
unsigned char index, n;
unsigned int address = 0;
FILE *bin, *text;
struct stat statbuf;
printf("Hello, world\n");
if (argc != 3)
{
fprintf(stderr, "请在命令行上指定两个文件,格式为:\n%s binaryFile textFile\n", *argv);
exit(-1);
}
if((bin = fopen(*++argv, "rb")) == NULL) {
fprintf(stderr, "输入文件 %s 打开错误!", *argv);
exit(-2);
}
if((text = fopen(*++argv, "wt")) == NULL) {
fprintf(stderr, "输出文件 %s 打开错误!", *argv);
exit(-3);
}
fstat(fileno(bin), &statbuf);
printf("文件长 %u(%#.2f KB) 字节。\n", statbuf.st_size, (float)statbuf.st_size / 1024.0F);
while(!feof(bin) && !ferror(bin)) {
for(n = 0; n < RWMAXBUFLEN - 1; n++) {
ch = fgetc(bin);
if(feof(bin) || ferror(bin))
break;
rbuf[n] = ch;
//fprintf(text, "%02x ", ch & 0x00FF);
wbuf[n] = isprint(ch) ? ch : '.';
}
if(n) {
fprintf(text, "[%#010x] ", address);
address += n;
for(index = 0; index < n; index++)
fprintf(text, "%02x ", rbuf[index] & 0x00FF);
while(index++ < RWMAXBUFLEN - 1)
fprintf(text, "%3c", 32);
fprintf(text, "%s\n", wbuf);
memset(wbuf, 0, sizeof(wbuf));
memset(rbuf, 0, sizeof(rbuf));
}
}
printf("共转换了 %u 字节。\n", address);
if((unsigned int)(statbuf.st_size) == address)
printf("转换完毕。\n");
else
fprintf(stderr, "转换出现错误。\n");
fclose(bin);
fclose(text);
return 0;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -