数据格式转换简单实现
1. 把内存里十六进制字符串转为整数int HexToInt(const char* pstr_str,int li_len)
{
char s[4];
int i, x=li_len-1;
if(li_len >4 || li_len < 1)
{
return -1;
}
memset(s,0,4);
for (i=0;i<li_len;i++)
{
memcpy(&s[i],&pstr_str[x],1);
x--;
}
x=0;
memcpy(&x,s,4);
return x;
}
2.十六进制转换为字符串
int HexToStr(unsigned const char *pstr_HexData,const int Length,char *pstr_StrData)
{
int i=0;
unsigned char lz_tmp[4];
for(i=0;i<Length;i++)
{
memset(lz_tmp,0,2);
sprintf(lz_tmp,"%02X",pstr_HexData[i]);
strcat(pstr_StrData,lz_tmp);
}
return 0;
}
3.BCD进制转换成字符串
int BCDToASC(unsigned const char *pstr_BCDData,const int Length,char *pstr_ASCData)
{
int i=0;
unsigned char H,L;
unsigned char tmp[4];
for(i=0;i<Length;i++)
{
H=pstr_BCDData[i]&0x0F;
L=pstr_BCDData[i]&0xF0;
L=L>>4;
memset(tmp,0,4);
sprintf(tmp,"%0X",H);
strcat(pstr_ASCData,tmp);
if(L == 0xF)
{
break;
}
sprintf(tmp,"%0X",L);
strcat(pstr_ASCData,tmp);
}
return 0;
}
4.十六进制转换成ASC
int HexToASC(unsigned const char *pstr_HexData,const int Length,char *pstr_ASCData)
{
int i=0;
int li_val = 0;
char lz_tmp[3]={0};
for(i=0;i<Length;i++)
{ www.zzzyk.com
strncpy(lz_tmp,pstr_HexData+i,1);
li_val = HexToInt(lz_tmp,1);
pstr_ASCData[i] = (char)li_val;
}
return 0;
}
补充:综合编程 , 其他综合 ,