我用的将16进制文本转换为ascii码的C语言代码。16进制文本复制到文件test.txt中,运行程序,将得到输入。
例:
运行:./CSTRING test.txt
其中,test.txt文件内容为:
30:31:32
则得到的"Cstring.txt"输入文件中
012
本程序能自动跳过0或多个分隔符(分隔符可以为任意的符号,如“:”,“%”或空格等,判断的函数实现见SkipDelimiter函数)。
如:输入文件内容为:
30 31 32
或
303132
得到的都是
012
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <libgen.h>
#include <fcntl.h>
#include <stdlib.h>
typedef unsigned char uint8;
uint8 ConverseOneChar(uint8 ucChar)
{
uint8 ucTmp = 0x00;
printf("one ucChar= 0x%x\n", (char)ucChar);
switch(ucChar)
{
case '0':
ucTmp = 0x00;
break;
case '1':
ucTmp = 0x01;
break;
case '2':
ucTmp = 0x02;
break;
case '3':
ucTmp = 0x03;
break;
case '4':
ucTmp = 0x04;
break;
case '5':
ucTmp = 0x05;
break;
case '6':
ucTmp = 0x06;
break;
case '7':
ucTmp = 0x07;
break;
case '8':
ucTmp = 0x08;
break;
case '9':
ucTmp = 0x09;
break;
case 'a':
case 'A':
ucTmp = 0x0a;
break;
case 'b':
case 'B':
ucTmp = 0x0b;
break;
case 'c':
case 'C':
ucTmp = 0x0c;
break;
case 'd':
case 'D':
ucTmp = 0x0d;
break;
case 'e':
case 'E':
ucTmp = 0x0e;
break;
case 'f':
case 'F':
ucTmp = 0x0f;
break;
default:
printf("err char!\n");
return -1;
}
return ucTmp;
}
uint8 GetOneChar(uint8 *pucBuf)
{
uint8 ucChar = 0x00;
if(NULL == pucBuf)
{
printf("para is NULL!\n");
return -1;
}
ucChar = ConverseOneChar(pucBuf[0]);
printf("first ucChar= 0x%x\n", (char)ucChar);
ucChar <<= 4;
ucChar |= ConverseOneChar(pucBuf[1]);
printf("second ucChar= %c[0x%x]\n", (char)ucChar, ucChar);
return ucChar;
}
int SkipDelimiter(uint8 *pcBuf)
{
int n = 0;
char *pcBufTmp = (char *)pcBuf;
for(n = 0; (pcBufTmp[n] != '\0') && (pcBufTmp[n] != '\n') && (pcBufTmp[n] != '\r'); n++)
{
if ((pcBufTmp[n] == '0')
|| (pcBufTmp[n] == '1')
|| (pcBufTmp[n] == '2')
|| (pcBufTmp[n] == '3')
|| (pcBufTmp[n] == '4')
|| (pcBufTmp[n] == '5')
|| (pcBufTmp[n] == '6')
|| (pcBufTmp[n] == '7')
|| (pcBufTmp[n] == '8')
|| (pcBufTmp[n] == '9')
|| (pcBufTmp[n] == 'A') || (pcBufTmp[n] == 'a')
|| (pcBufTmp[n] == 'B') || (pcBufTmp[n] == 'b')
|| (pcBufTmp[n] == 'C') || (pcBufTmp[n] == 'c')
|| (pcBufTmp[n] == 'D') || (pcBufTmp[n] == 'd')
|| (pcBufTmp[n] == 'E') || (pcBufTmp[n] == 'e')
|| (pcBufTmp[n] == 'F') || (pcBufTmp[n] == 'f'))
{
break;
}
}
return n;
}
int ParseHexString(unsigned char *pucSrcBuf, int iSrcFileLen, unsigned char *pucDstBuf)
{
uint8 *pucSrcTmp = NULL;
uint8 *pucDstTmp = NULL;
uint8 ucTmp = 0;
int iFileLenRemain = iSrcFileLen;
int iDelimiterNum = 0;
int n = 0;
if((NULL == pucSrcBuf)||(NULL == pucSrcBuf))
{
printf("para 1 or 2 is NULL!\n");
return -1;
}
pucSrcTmp = pucSrcBuf;
pucDstTmp = pucDstBuf;
while(iFileLenRemain != 0)
{
ucTmp = GetOneChar(pucSrcTmp);
printf("ucTmp = %c\n", (char)ucTmp);
pucDstTmp[n++] = ucTmp;
pucSrcTmp += 2;
iDelimiterNum = SkipDelimiter(pucSrcTmp);
printf("iDelimiterNum=%d\n", iDelimiterNum);
pucSrcTmp += iDelimiterNum;
iFileLenRemain -= 2 + iDelimiterNum;
}
return n;
}
int main(int argc, char **argv)
{
int iRet = 0;
char *pcFileForChange = NULL;
int iSrcFd = 0;
int iDstFd = 0;
int iSrcFileLen = 0;
int n = 0;
unsigned char *pucSrcBuf = NULL;
unsigned char *pucDstBuf = NULL;
if(argc != 2)
{
printf("you need input the filename to change!\n");
return -1;
}
pcFileForChange = argv[1];
printf("filename:%s\n", pcFileForChange);
iSrcFd = open(pcFileForChange, O_RDONLY);
if (iSrcFd < 0)
{
printf("open file:[%s] faild!\n", pcFileForChange);
return -1;
}
iDstFd = open("Cstring.txt", O_RDWR | O_CREAT | O_TRUNC, 0644);
if (iDstFd < 0)
{
printf("open change filename file faild!\n");
return -1;
}
iSrcFileLen = lseek(iSrcFd, 0, SEEK_END);
printf("FileLen:%d\n", iSrcFileLen);
lseek(iSrcFd, 0, SEEK_SET);
pucSrcBuf = malloc(iSrcFileLen + 1024);
pucDstBuf = malloc(iSrcFileLen);
memset(pucSrcBuf, 0, iSrcFileLen);
memset(pucDstBuf, 0, iSrcFileLen);
iRet = read(iSrcFd, pucSrcBuf, iSrcFileLen);
if(iRet != iSrcFileLen)
{
printf("read err, read len = %d\n", iRet);
return -1;
}
printf("read:%s\n", pucSrcBuf);
n = ParseHexString(pucSrcBuf, iSrcFileLen, pucDstBuf);
printf("write:\n%s\n", pucDstBuf);
iRet = write(iDstFd, pucDstBuf, n);
if(iRet != n)
{
printf("write err, write len = %d\n", iRet);
return -1;
}
close(iSrcFd);
close(iDstFd);
return 0;
}