#include <stdio.h>
#include <string.h>
#include <stdlib.h>
unsigned long HEX2DEC(const char *s)
{
int i,t;
unsigned long sum=0;
for (i=0; s[i]; i++)
{
if ( (s[i]=='0' && s[i+1]=='x') || (s[i]=='\\' && s[i+1]=='x') )
{
i++;
continue; //0xff, \x64, 去前缀
}
if ( s[i]>='0' && s[i] <= '9' )
t = s[i]-'0';
else if ( s[i]>='A' && s[i] <= 'F')
t = s[i]-'A'+10;
else if ( s[i]>='a' && s[i]<='f' )
t = s[i]-'a'+10;
else
break;
sum=sum*16+t;
}
return sum;
}
int main(void)
{
FILE *fd = fopen("d:/1.txt", "r");
fseek(fd,0,SEEK_END);
unsigned long s = ftell(fd);
char *p = (char *)malloc(s);
memset(p, 0, s );
fseek(fd,0,SEEK_SET);
fread( p, s, 1, fd);
fclose(fd);
// output
char t[16]={0}, n[2]={0};
fd = fopen("d:/2.txt", "w+");
for ( unsigned int i=0; i<strlen(p); i++ )
{
if ( p[i] == '\\' && p[i+1] == 'x' )
{
sprintf(t, "%c%c", p[i+2], p[i+3]);
fputc(HEX2DEC(t),fd);
i += 3;
continue;
}
fputc(p[i], fd);
}
fclose(fd);
free(p);
return 0;
}
转载于:https://my.oschina.net/guyson/blog/233044