歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux編程 >> Linux編程

Linux C 源碼(nMAsciiHexToBinary:將16進制字符串格式轉換為ASCII碼形式)

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 */

Copyright © Linux教程網 All Rights Reserved