//函数名:GsmByte2String
//描述:二进制数据转换为字符串,如二进制//数:0x0A 0x6B 0xFF 0xB6 0x12 转为//字符串:“0A6BFFB612”
//参数:const unsigned char* pSrc 二进制数据
// char* pDst 转换后的目标字符串
// int nSrcLength 二进制数据长度
//返回值:转换完之后,字符串的长度
int GsmByte2String(const unsigned char* pSrc,char* pDst,int nSrcLength)
{
const char tab[]="0123456789ABCDEF";
for(int i=0;i<nSrcLength;i++)
{
*pDst++ = tab[*pSrc>>4];
*pDst++=tab[*pSrc & 0x0f];
pSrc++;
}
*pDst = '\0';
return nSrcLength*2;
}
unsigned char convertChar2Byte(char psrc)
{
unsigned char tempbyte = 0x00;
if(psrc>='0' && psrc<='9')
{
tempbyte = psrc-'0';
}
else if(psrc=='A' || psrc=='a')
{
tempbyte = 0x0A;
}
else if(psrc=='B' || psrc=='b')
{
tempbyte = 0x0B;
}
else if(psrc=='C' || psrc=='c')
{
tempbyte = 0x0C;
}
else if(psrc=='D' || psrc=='d')
{
tempbyte = 0x0D;
}
else if(psrc=='E' || psrc=='e')
{
tempbyte = 0x0E;
}
else if(psrc=='F' || psrc=='f')
{
tempbyte = 0x0F;
}
return tempbyte;
}
unsigned char convertTwoChar2Byte(char hchar,char lchar)
{
unsigned char tempbyte = 0x00;
unsigned char hbyte = convertChar2Byte(hchar);
unsigned char lbyte = convertChar2Byte(lchar);
tempbyte = tempbyte | hbyte;
tempbyte = tempbyte<<4;
tempbyte = tempbyte | lbyte;
return tempbyte;
}
//函数名:gsmString2Bytes
//描述:字符串转换成二进制数据 如字符串:“0A6BFFB612” 转为二进制数据:0x0A 0x6B 0xFF 0xB6 0x12
//参数:const char* pSrc 字符串数据
// unsigned char* pDst 转换之后的二进制数据
// nSrcLength 字符串长度
//返回值:转换完之后,二进制数据个数
int gsmString2Bytes(const char* pSrc,unsigned char* pDst,int nSrcLength)
{
for(int i=0;i<nSrcLength;i=i+2)
{
unsigned char thebyte = convertTwoChar2Byte(pSrc[i],pSrc[i+1]);
*pDst++ = thebyte;
}
return nSrcLength/2;
}
unsigned char convertChar2Byte(char psrc)
{
unsigned char tempbyte = 0x00;
if(psrc>='0' && psrc<='9')
{
tempbyte = psrc-'0';
}
else if(psrc=='A' || psrc=='a')
{
tempbyte = 0x0A;
}
else if(psrc=='B' || psrc=='b')
{
tempbyte = 0x0B;
}
else if(psrc=='C' || psrc=='c')
{
tempbyte = 0x0C;
}
else if(psrc=='D' || psrc=='d')
{
tempbyte = 0x0D;
}
else if(psrc=='E' || psrc=='e')
{
tempbyte = 0x0E;
}
else if(psrc=='F' || psrc=='f')
{
tempbyte = 0x0F;
}
return tempbyte;
}
unsigned char convertTwoChar2Byte(char hchar,char lchar)
{
unsigned char tempbyte = 0x00;
unsigned char hbyte = convertChar2Byte(hchar);
unsigned char lbyte = convertChar2Byte(lchar);
tempbyte = tempbyte | hbyte;
tempbyte = tempbyte<<4;
tempbyte = tempbyte | lbyte;
return tempbyte;
}
//函数名:gsmString2Bytes
//描述:字符串转换为二进制数据,如字符串:“0A6BFFB612” 转为 二进制数据:0x0A 0x6B 0xFF 0xB6 0x12
//参数:const char* pSrc 字符串数据
// unsigned char* pDst 转换完的二进制数据
// int nSrcLength 字符串长度
//返回值:转换完之后,二进制数据的个数
int gsmString2Bytes(const char* pSrc,unsigned char* pDst,int nSrcLength)
{
for(int i=0;i<nSrcLength;i=i+2)
{
unsigned char thebyte = convertTwoChar2Byte(pSrc[i],pSrc[i+1]);
*pDst++ = thebyte;
}
return nSrcLength/2;
}