#define CHAR_TO_UPPER(ch) ((ch >= 'a' && ch <= 'z')?(ch-0x20):ch)
/**
* @brief ascii convert hex
* @par param[in] *hex:hex data
* @par param[in] *ascii:ascii data
* @par param[in] asciiLen:length of ascii
* @retval length
*/
u8 Ascii2Hex(u8 *hex, u8 *ascii, u8 asciiLen)
{
u8 i,ch,value;
value = 0;
for(i=0;i<(asciiLen>>1);i++) {
ch = CHAR_TO_UPPER(ascii[i*2]);
if(ch >= '0' && ch <= '9') {
value = ch -'0';
}
else if(ch >= 'A' && ch <= 'F') {
value = ch - 'A' + 0x0A;
}
else {
return i;
}
hex[i] = (value<<4);
ch = CHAR_TO_UPPER(ascii[i*2+1]);
if(ch >= '0' && ch <= '9') {
value = ch -'0';
}
else if(ch >= 'A' && ch <= 'F') {
value = ch - 'A' + 0x0A;
}
else {
return i;
}
hex[i] += value;
}
return i;
}
/**
* @brief hex convert ascii
* @par param[in] *ascii:ascii data
* @par param[in] *hex:hex data
* @par param[in] hexLen:length of hex
* @retval length
*/
u8 Hex2Ascii(u8 *ascii, u8 *hex, u8 hexLen)
{
u8 i, value;
for(i=0;i<hexLen;i++) {
value = (hex[i]>>4);
if(value > 9) {
value += 0x07;
}
ascii[2*i] = value+0x30;
value = (hex[i]&0x0F);
if(value > 9) {
value += 0x07;
}
ascii[2*i+1] = value+0x30;
}
return hexLen*2;
}