unsigned char HEX2BYTE(unsigned char hex_ch)
{
if (hex_ch >= '0' && hex_ch <= '9')
{
return hex_ch - '0';
}
if (hex_ch >= 'a' && hex_ch <= 'f')
{
return hex_ch - 'a' + 10;
}
if (hex_ch >= 'A' && hex_ch <= 'F')
{
return hex_ch - 'A' + 10;
}
return 0x00;
}
unsigned char _4BIT2HEX(unsigned char part_cn)
{
if (part_cn >= 10) {
return (unsigned char )(part_cn - 10 + 'A');
} else {
return (unsigned char )(part_cn + '0');
}
}
void StrToHex(const unsigned char* in, long inlen, unsigned char* out, long* outlen)
{
*outlen = inlen/2;
memset(out, 0, *outlen);
for (int i = 0; i < *outlen; i ++) {
out[i] = (HEX2BYTE(in[i*2]) << 4) | HEX2BYTE(in[i*2 + 1]);
}
}
void HexToStr(const unsigned char* in, long inlen, unsigned char* out, long* outlen)
{
*outlen = inlen*2;
memset(out, 0, *outlen + 1);
for (int i = 0, j = 0; i < *outlen; i ++, i++, j++) {
out[i] = _4BIT2HEX(((in[j]) & 0xF0)>>4);
out[i+1] = _4BIT2HEX(((in[j]) & 0x0F));
}
}