Linux C 源码(nMAsciiHexToBinary:将16进制字符串格式转换为

Linux C 源码(nMAsciiHexToBinary:将16进制字符串格式转换为ASCII码形式)

short nMAsciiHexToBinary(
unsigned char* vspSourceStr,
short          vnSourceStrL,
unsigned char* vspDestStr) {


if(vnSourceStrL % 2)
return -1;


for(; vnSourceStrL > 0; vnSourceStrL -= 2) {
if(*vspSourceStr >= '0' &&
*vspSourceStr <= '9')
*vspDestStr = ((*vspSourceStr++) - '0') * 16;
else if(*vspSourceStr >= 'a' &&
*vspSourceStr <= 'f')
*vspDestStr = ((*vspSourceStr++) - 'a' + 10) * 16;
else if(*vspSourceStr >= 'A' &&
*vspSourceStr <= 'F')
*vspDestStr = ((*vspSourceStr++) - 'A' + 10) * 16;
else
return -1;
if(*vspSourceStr >= '0' &&
*vspSourceStr <= '9')
(*vspDestStr++) += ((*vspSourceStr++) - '0');
else if(*vspSourceStr >= 'a' &&
*vspSourceStr <= 'f')
(*vspDestStr++) += ((*vspSourceStr++) - 'a' + 10);
else if(*vspSourceStr >= 'A' &&
*vspSourceStr <= 'F')
(*vspDestStr++) += ((*vspSourceStr++) - 'A' + 10);
else
return -1;
} /* end of for */


return 0;
} /* end of nMAsciiHexToBinary */

相关推荐