类型转换的实现代码(C++)
byte CFormat_Input::ASCII_Hex_to_Byte(char *ascii_hex)
{
int i;
char cTemp;
byte byReturn;
byReturn=0;
for (i=0; i < 2; i++)
{
cTemp=ascii_hex[i];
if ( cTemp == 0x00 )
break;
byReturn=byReturn<<4;
if ( (cTemp >= '0')&&(cTemp <='9') )
byReturn+=cTemp-'0';
else
{
cTemp|=0x20;
if ( (cTemp >= 'a')&&(cTemp <='f') )
byReturn+=cTemp-'a'+0x0a;
}
}
return byReturn;
}
void CFormat_Input::Hex_2_Bin_format(char *szDes, byte bySrc)
{
char *Hex_2_Bin_array[]={
"0000",
"0001",
"0010",
"0011",
"0100",
"0101",
"0110",
"0111",
"1000",
"1001",
"1010",
"1011",
"1100",
"1101",
"1110",
"1111",
};
sprintf(&szDes[0], "%s", Hex_2_Bin_array[(int)bySrc/0x10]);
sprintf(&szDes[4], "%s", Hex_2_Bin_array[(int)bySrc%0x10]);
}
unsigned long CFormat_Input::ASCII_To_long(byte *bySrc, int nInput_length, byte bySwitch)
{
int i;
unsigned long ulResult;
if (nInput_length >= 5)
return 0x00;
ulResult=0;
switch (bySwitch)
{
case 0x00:
{
for (i=0; i < nInput_length; i++)
{
ulResult=(ulResult<<8)+bySrc[i];
}
break;
}
case 0x01:
{
for (i=0; i < nInput_length; i++)
{
ulResult=(ulResult<<8)+bySrc[nInput_length-i-1];
}
break;
}
}
return ulResult;
}