首页 > 代码库 > HEX与ASCII之间装换

HEX与ASCII之间装换

static const char bcd_ascll[20]={0x30,0x31,0x32,0x33,0x34,

                                              0x35,0x36,0x37,0x38,0x39,

                                             0X41,0X42,0X43,0X44,0X45,0X46};

const char ascll_bcd[25]={      0x00,0x01,0x02,0x03,0x04,0x05,

                                             0x06,0x07,0x08,0x09,0x00,0x00,

                                             0x00,0x00,0x00,0x00,0x00,0x0A,

                                             0X0B,0X0C,0X0D,0X0E,0X0F};

将 Data = http://www.mamicode.com/0x42转换成 ASCII:

AsciiData[0] = bcd_ascll[((bcd_ascll>>4)&oxf)] = 0x34

AsciiData[1] = bcd_ascll[((bcd_ascll)&oxf)];      = 0x32

 

AsciiData= {0x34,0x35}转换成 BCD:

Data    = ascll_bcd[((AsciiData[0]-0x30)<<4)] = 0x40;

Data += ascll_bcd[((AsciiData[1]-0x30))] = 0x45;

 

HEX与ASCII之间装换