为解决一个网友的问题刚写的,16进制字符文本/二进制文件迷你互转器
(由于带有回车换行选择性处理的开关,加上0D 0A可以是要处理的二进制数据,也可以是回车换行,所以虽说是小意思,但写的过程中还是颇费一些心思的。。)
对学习字符,数值处理很有帮助的。。。
加了,简单的输入检测和帮助
更新了。
readme.txt
***********************
编译工具:TC3 VC6
语法:
txt2bin 文本文件名 二进制文件名 [/killline|n]
/killline 去除回车换行
n 以每隔n对16进制字符添加一个回车换行
例子:
txt2bin file.txt file.bin
txt2bin file.txt file.bin /killline
txt2bin file.txt file.bin 10
bin2txt 二进制文件名 文本文件名 [/killline|n] [/lower]
/killline 去除回车换行
n 以每隔n个字节添加一个回车换行
/lower 以小写方式
例子:
bin2txt file.bin file.txt
bin2txt file.bin file.txt /killline
bin2txt file.bin file.txt 10
bin2txt file.bin file.txt 10 /lower
/***************** txt2bin.c 16进制字符文件转为二进制文件 ******************/
#include <io.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void help()
{
printf("txt2bin 1.0 HexTextToBin convertor by grub2linux\nSyntax :\n txt2bin textFile binFile [/killline|n]\n textFile : text file name.\n binFile : binary file name.\n /killline : do not conserve RETURN & LINEFEEDs in textFile.\n n : do not conserve RETURN & LINEFEEDs in textFile.\n add a RETURN & LINEFEED every n*2 characters.\n");
}
void main(int argc, char *argv[])
{
FILE *txt,*bin;
unsigned short dig;
unsigned char w[3];
unsigned long i=0;
unsigned long j=0;
unsigned char buf;
char *stopStr="\0";
unsigned long filesize;
int doLine=0;
w[2]=0;
switch(argc)
{
case 4 :
if(strcmp(strupr(argv[3]),"/KILLLINE")==0) doLine=-1;
else doLine=atoi(argv[3]);
case 3 :
if((txt=fopen(argv[1],"rb"))==NULL)
{
printf("File error : %s\n",argv[1]);
return;
}
if((bin=fopen(argv[2],"wb"))==NULL)
{
printf("File error : %s\n",argv[2]);
return;
}
break;
default :
help();
return;
}
filesize=filelength(fileno(txt));
while(i<filesize)
{
fread(&dig,2,1,txt);
i+=2;
// printf("%04x\n",dig);
if(dig==0x0A0D)
{
if(doLine==0)
fwrite("\x0D\x0A",2,1,bin);
}
else
{
memcpy(w,&dig,2);
buf=(unsigned char)strtol(w,&stopStr,16);
fwrite(&buf,1,1,bin);
j++;
if(doLine>0 && j%doLine==0) fwrite("\x0D\x0A",2,1,bin);
}
}
fclose(txt);
fclose(bin);
}
/***************** bin2txt.c 二进制文件转为16进制字符文件 *******************/
#include <ctype.h>
#include <io.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
void help()
{
printf("bin2txt 1.0 BinToHexText convertor by grub2linux\nSyntax :\n bin2txt binFile textFile [/killline|n] [/lower]\n binFile : binary file name.\n textFile : text file name.\n /killline : do not conserve RETURN & LINEFEEDs in binFile.\n n : do not conserve RETURN & LINEFEEDs in binFile.\n add a RETURN & LINEFEED every n bytes.\n /lower : in lower case.\n");
}
void main(int argc, char *argv[])
{
FILE *txt,*bin;
unsigned char dig;
unsigned char dig0A;
char w[3];
unsigned long i=0;
unsigned long j=0;
unsigned long filesize;
int doLine=0;
int letterCase=0;
w[2]=0;
switch(argc)
{
case 5 :
if(strcmp(strupr(argv[4]),"/LOWER")==0) letterCase=1;
else
{
help();
return;
}
case 4 :
if(strcmp(strupr(argv[3]),"/KILLLINE")==0) doLine=-1;
else if(strcmp(strupr(argv[3]),"/LOWER")==0) letterCase=1;
else doLine=atoi(argv[3]);
case 3 :
if((bin=fopen(argv[1],"rb"))==NULL)
{
printf("File error : %s\n",argv[1]);
return;
}
if((txt=fopen(argv[2],"wb"))==NULL)
{
printf("File error : %s\n",argv[2]);
return;
}
break;
default :
help();
return;
}
filesize=filelength(fileno(bin));
while(i<filesize)
{
fread(&dig,1,1,bin);
i++;
if(dig==0x0D)
{
fread(&dig0A,1,1,bin);
i++;
if(dig0A==0x0A)
{
if(doLine==0)
fwrite("\x0D\x0A",2,1,txt);
}
else
{
if(letterCase==0)
sprintf(w,"%02X",dig);
else sprintf(w,"%02x",dig);
fwrite(w,2,1,txt);
j++;
if(doLine>0 && j%doLine==0) fwrite("\x0D\x0A",2,1,txt);
if(letterCase==0)
sprintf(w,"%02X",dig0A);
else sprintf(w,"%02x",dig0A);
fwrite(w,2,1,txt);
j++;
if(doLine>0 && j%doLine==0) fwrite("\x0D\x0A",2,1,txt);
// fseek(bin,-1L,1);
}
}
else
{
if(letterCase==0)
sprintf(w,"%02X",dig);
else sprintf(w,"%02x",dig);
fwrite(w,2,1,txt);
j++;
if(doLine>0 && j%doLine==0) fwrite("\x0D\x0A",2,1,txt);
// itoa((unsigned char)dig,w,16);
/*
itoa((unsigned char)(dig>>8),w,16);
strcpy(w,strupr(w));
fwrite(w,2,1,txt);
*/
}
}
fclose(txt);
fclose(bin);
}
[此贴子已经被作者于2006-7-20 21:26:38编辑过]