|
单片机C语言的十六进制(HEX)转BCD码程序
[复制链接]
本帖最后由 tiankai001 于 2018-3-23 11:08 编辑
此内容由EEWORLD论坛网友tiankai001原创,如需转载或用于商业用途需征得作者同意并注明出处
说明:本程序分别列举了单字节HEX码转换为BCD型数据;2字节HEX码转换为BCD型数据;4字节HEX码转换为BCD型数据的的方法。
- void HEXtoBCDData(void)
- {
- unsigned char ucHEXdata[10] = {0},ucBCDdata[10] = {0};
- unsigned int uiHEXdata = 0;
- unsigned long int ulHEXdata = 0;
- //[color=#00ff][size=4]单字节HEX码转换为BCD型数据[/size][/color]
- ucHEXdata[0] = 0x63; //十进制为99
- ucBCDdata[0] = (ucHEXdata[0]/10)*0x10 + (ucHEXdata[0]%10);
- //ucBCDdata[0]=99
- ucHEXdata[0] = 0xf5; //十进制为245
- ucHEXdata[1] = ucHEXdata[0]/100;
- ucHEXdata[2] = ucHEXdata[0]%100;
- ucBCDdata[0] = ucHEXdata[1];
- ucBCDdata[1] = (ucHEXdata[2]/10)*0x10 + (ucHEXdata[2]%10);
- //ucBCDdata[1]=2,ucBCDdata[0]=45
- //[color=#00ff][size=4]2字节HEX码转换为BCD型数据[/size][/color]
- uiHEXdata = 0xfff5; //十进制为65525
- ucHEXdata[1] = uiHEXdata%100;
- ucBCDdata[0] = (ucHEXdata[1]/10)*0x10 + (ucHEXdata[1]%10);
- uiHEXdata = uiHEXdata/100;
- ucHEXdata[2] = uiHEXdata%100;
- ucBCDdata[1] = (ucHEXdata[2]/10)*0x10 + (ucHEXdata[2]%10);
- uiHEXdata = uiHEXdata/100;
- ucHEXdata[3] = uiHEXdata%100;
- ucBCDdata[2] = (ucHEXdata[3]/10)*0x10 + (ucHEXdata[3]%10);
- //ucBCDdata[2]=6,ucBCDdata[1]=55,ucBCDdata[0]=25
- //[color=#00ff][size=4]4字节HEX码转换为BCD型数据[/size][/color]
- ulHEXdata = 0xfffffff5; //十进制为4294967285
- ucHEXdata[1] = ulHEXdata%100;
- ucBCDdata[0] = (ucHEXdata[1]/10)*0x10 + (ucHEXdata[1]%10);
- ulHEXdata = ulHEXdata/100;
- ucHEXdata[2] = ulHEXdata%100;
- ucBCDdata[1] = (ucHEXdata[2]/10)*0x10 + (ucHEXdata[2]%10);
- ulHEXdata = ulHEXdata/100;
- ucHEXdata[3] = ulHEXdata%100;
- ucBCDdata[2] = (ucHEXdata[3]/10)*0x10 + (ucHEXdata[3]%10);
- ulHEXdata = ulHEXdata/100;
- ucHEXdata[4] = ulHEXdata%100;
- ucBCDdata[3] = (ucHEXdata[4]/10)*0x10 + (ucHEXdata[4]%10);
- ulHEXdata = ulHEXdata/100;
- ucHEXdata[5] = ulHEXdata%100;
- ucBCDdata[4] = (ucHEXdata[5]/10)*0x10 + (ucHEXdata[5]%10);
- //ucBCDdata[4]=42,ucBCDdata[3]=94,
- //ucBCDdata[2]=96,ucBCDdata[1]=72,ucBCDdata[0]=85
- ulHEXdata = 0xfffffff4; //十进制为42 94 96 72 84
- for(int i=0;i<5;i++)
- {
- ucHEXdata[i]=ulHEXdata%100;
- ulHEXdata=ulHEXdata/100;
- ucBCDdata[i]=(ucHEXdata[i]/10)*0x10+ucHEXdata[i]%10;
- }
- //ucBCDdata[4]=42,ucBCDdata[3]=94,
- //ucBCDdata[2]=96,ucBCDdata[1]=72,ucBCDdata[0]=84
- }
复制代码
|
|