This post was last edited by tiankai001 on 2018-3-23 10:58 Description: This program lists the methods of converting single-byte BCD code to char type data; converting 2-byte BCD code to int type data; and converting 4-byte BCD code to long int type data.
unsigned char ucBCDdata[10] = {0x11,0x22,0x33,0x44,0x55,0x55,0x66,0x77};
unsigned char ucHEXdata11 = 0,ucHEXdata[10] = {0};
unsigned int uiHEXdata = 0,uiHEXdata11 = 0;
unsigned long int ulHEXdata = 0;
//1. In reality, hexadecimal numbers and decimal numbers cannot be added directly. If it is on a computer, it is possible, because no matter what base number is represented in binary form on a computer, it can be added when writing a program on a computer.
//In C language programs, decimal and hexadecimal are actually interchangeable,
//Single-byte BCD code to decimal
//High half byte = quotient of BCD code byte divided by 16, low half byte = remainder of BCD code byte divided by 16
ucHEXdata[0] = (ucBCDdata[0]>>4)*10 +(0xf & ucBCDdata[0]);
ucHEXdata11 = ucHEXdata[0];
//Convert 2-byte BCD code to integer
//First convert the 2-byte BCD code to decimal, then high byte*100+low byte=integer
ucHEXdata[0] = (ucBCDdata[0]>>4)*10 +(0xf & ucBCDdata[0]);
ucHEXdata[1] = (ucBCDdata[1]>>4)*10 +(0xf & ucBCDdata[1]);
uiHEXdata = ucHEXdata[1]*100 + ucHEXdata[0];
//Convert 4 bytes of BCD code to integer
//First convert the 4 bytes of BCD code to decimal respectively, and integrate them into long integer according to the decimal integration method
ucHEXdata[0] = (ucBCDdata[0]>>4)*10 +(0xf & ucBCDdata[0]);
ucHEXdata[1] = (ucBCDdata[1]>>4)*10 +(0xf & ucBCDdata[1]);
ucHEXdata[2] = (ucBCDdata[2]>>4)*10 +(0xf & ucBCDdata[2]);
ucHEXdata[3] = (ucBCDdata[3]>>4)*10 +(0xf & ucBCDdata[3]);
ulHEXdata = ((unsigned long int)ucHEXdata[3]*1000000)
+((unsigned long int)ucHEXdata[2]*10000)
+((unsigned long int)ucHEXdata[1]*100) + ucHEXdata[0];