|
发表于 2010-6-12 18:17:32
|
显示全部楼层
(原文件名:src.jpg)
转换程序:
#include <stdio.h>
int Hex2Ascii(char* hex, unsigned char* ascii)
{
int len = strlen(hex), tlen, i, cnt;
for (i = 0, cnt = 0, tlen = 0; i<len; i++)
{
char c = toupper(hex);
if ((c>='0'&& c<='9') || (c>='A'&& c<='F'))
{
unsigned char t = (c >= 'A') ? c - 'A' + 10 : c - '0';
if (cnt)
ascii[tlen++] += t, cnt = 0;
else
ascii[tlen] = t << 4, cnt = 1;
}
}
return tlen;
}
int main(void)
{
char c;
unsigned int i,len,len_w;
char * Rbuf=NULL;
unsigned char * Wbuf=NULL;
FILE *fp;
if((fp = fopen("src.txt", "r"))== NULL)
{
printf("Cannot open file.\n");
return -1;
}
for(i = 0;!feof(fp);i++)
{
fread(&c,1,1,fp);
}
fclose(fp);
len=i-1;
Rbuf=(char *) malloc(len+1);
fp = fopen("src.txt", "r");
fread(Rbuf,len,1,fp);
fclose(fp);
Wbuf=(unsigned char *) malloc((len/3)+1);
len_w=Hex2Ascii(Rbuf,Wbuf);
if((fp = fopen("src.jpg", "wb"))== NULL)
{
printf("Cannot create file.\n");
return -1;
}
fwrite(Wbuf,len_w,1,fp);
fclose(fp);
free(Rbuf);
free(Wbuf);
return 0;
} |
|