求ASCII转HEX和HEX转ASCII的算法
如题 求你给我点钱 好象超简单的。 取之于该网站,服务于该网站。下面是我在该网站上找的,希望对你有用。void hex_to_ascii(unsigned char dat)
{
unsigned char dat_buff;
dat_buff = dat;
dat = dat&0x0f;
if((dat>=0)&&(dat<=9))
{
dat += 0x30;
}
else
dat += 0x37;
UART_TX_buff = dat;
dat = dat_buff;
dat >>= 4;
dat = dat&0x0f;
if((dat>=0)&&(dat<=9))
{
dat += 0x30;
}
else
dat += 0x37;
UART_TX_buff = dat;
}
void ascii_to_hex(void)
{
unsigned char cnt,i;
for(cnt=0,i=0;cnt<6;cnt++,cnt++,i++)
{
Twi_Hex_Data = Twi_ASCI_Data;//先放ASCII的高位(四位)
Twi_Hex_Data <<= 4;
Twi_Hex_Data += Twi_ASCI_Data;
}
} 记号,谢谢 谢谢大家哈, 这种问题应该自己多考虑的!多查查资料,建议! 记号 #define Hex2Ascii(data)(data < 10)? ('0' + data) : ('A' + data - 10)
void Byte2ASCII(unsigned char data, unsigned char *pBuf)
{
unsigned char temp;
temp = data & 0x0F;
pBuf = Hex2Ascii(temp);
temp = data >> 4;
pBuf = Hex2Ascii(temp);
}
这个短点 再加上两字节的:
#define Hex2Ascii(data)(data < 10)? ('0' + data) : ('A' + data - 10)
void Byte2ASCII(unsigned char data, unsigned char *pBuf)
{
unsigned char temp;
temp = data & 0x0F;
pBuf = Hex2Ascii(temp);
temp = data >> 4;
pBuf = Hex2Ascii(temp);
}
void Uint2ASCII(unsigned short data, unsigned char *pBuf)
{
unsigned char temp;
temp = (unsigned char)(data & 0x000F);
pBuf = Hex2Ascii(temp);
data >>= 4;
temp = (unsigned char)(data & 0x000F);
pBuf = Hex2Ascii(temp);
data >>= 4;
temp = (unsigned char)(data & 0x000F);
pBuf = Hex2Ascii(temp);
data >>= 4;
temp = (unsigned char)(data);
pBuf = Hex2Ascii(temp);
}
#define Ascii2Hex(data)(data >= '0' && data <= '9')? (data - '0') : ((data >= 'A' && data <= 'F')? (data - 'A' + 10) : ((data >= 'a' && data <= 'f')? (data - 'a' + 10) : 0))
unsigned char ASCII2Byte(unsigned char *pBuf)
{
unsigned char temp;
unsigned char c;
c = *pBuf++;
temp = ((unsigned char)Ascii2Hex(c)) << 4;
c = *pBuf;
temp += Ascii2Hex(c);
return temp;
}
unsigned short ASCII2Uint(unsigned char *pBuf)
{
unsigned short temp;
unsigned char c;
c = *pBuf++;
temp = ((unsigned short)Ascii2Hex(c)) << 12;
c = *pBuf++;
temp += ((unsigned short)Ascii2Hex(c)) << 8;
c = *pBuf++;
temp += ((unsigned short)Ascii2Hex(c)) << 4;
c = *pBuf;
temp += Ascii2Hex(c);
return temp;
} mark!要用到时再看 好东西,谢谢分享了,TO nicksean 不务正业,向你学习 nicksean 发表于 2008-4-19 09:49
再加上两字节的:
#define Hex2Ascii(data)(data < 10)? ('0' + data) : ('A' + data - 10)
void Byte2ASC ...
好算法,谢谢分享! 不客气,希望对你有用 这个算法很简单啊!自己写就好了!
页:
[1]