c语言hex转str和str转hex
void hex_str(unsigned char *inchar, unsigned int len, unsigned char *outtxt)
{
unsigned char hbit,lbit;
unsigned int i;
for(i=0;i<len;i++)
{
hbit = (*(inchar+i)&0xf0)>>4;
lbit = *(inchar+i)&0x0f;
if (hbit>9) outtxt[2*i]="A"+hbit-10;
else outtxt[2*i]="0"+hbit;
if (lbit>9) outtxt[2*i+1]="A"+lbit-10;
else outtxt[2*i+1]="0"+lbit;
}
outtxt[2*i] = 0;
}
unsigned int str_hex(unsigned char *str,unsigned char *hex)
{
unsigned char ctmp, ctmp1,half;
unsigned int num=0;
do{
do{
half = 0;
ctmp = *str;
if(!ctmp) break;
str++;
}while((ctmp == 0x20)||(ctmp == 0x2c)||(ctmp == " "));
if(!ctmp) break;
if(ctmp>="a") ctmp = ctmp -"a" + 10;
else if(ctmp>="A") ctmp = ctmp -"A"+ 10;
else ctmp=ctmp-"0";
ctmp=ctmp<<4;
half = 1;
ctmp1 = *str;
if(!ctmp1) break;
str++;
if((ctmp1 == 0x20)||(ctmp1 == 0x2c)||(ctmp1 == " "))
{
ctmp = ctmp>>4;
ctmp1 = 0;
}
else if(ctmp1>="a") ctmp1 = ctmp1 - "a" + 10;
else if(ctmp1>="A") ctmp1 = ctmp1 - "A" + 10;
else ctmp1 = ctmp1 - "0";
ctmp += ctmp1;
*hex = ctmp;
hex++;
num++;
}while(1);
if(half)
{
ctmp = ctmp>>4;
*hex = ctmp;
num++;
}
return(num);
}
{
unsigned char hbit,lbit;
unsigned int i;
for(i=0;i<len;i++)
{
hbit = (*(inchar+i)&0xf0)>>4;
lbit = *(inchar+i)&0x0f;
if (hbit>9) outtxt[2*i]="A"+hbit-10;
else outtxt[2*i]="0"+hbit;
if (lbit>9) outtxt[2*i+1]="A"+lbit-10;
else outtxt[2*i+1]="0"+lbit;
}
outtxt[2*i] = 0;
}
unsigned int str_hex(unsigned char *str,unsigned char *hex)
{
unsigned char ctmp, ctmp1,half;
unsigned int num=0;
do{
do{
half = 0;
ctmp = *str;
if(!ctmp) break;
str++;
}while((ctmp == 0x20)||(ctmp == 0x2c)||(ctmp == " "));
if(!ctmp) break;
if(ctmp>="a") ctmp = ctmp -"a" + 10;
else if(ctmp>="A") ctmp = ctmp -"A"+ 10;
else ctmp=ctmp-"0";
ctmp=ctmp<<4;
half = 1;
ctmp1 = *str;
if(!ctmp1) break;
str++;
if((ctmp1 == 0x20)||(ctmp1 == 0x2c)||(ctmp1 == " "))
{
ctmp = ctmp>>4;
ctmp1 = 0;
}
else if(ctmp1>="a") ctmp1 = ctmp1 - "a" + 10;
else if(ctmp1>="A") ctmp1 = ctmp1 - "A" + 10;
else ctmp1 = ctmp1 - "0";
ctmp += ctmp1;
*hex = ctmp;
hex++;
num++;
}while(1);
if(half)
{
ctmp = ctmp>>4;
*hex = ctmp;
num++;
}
return(num);
}
声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。