C program to convert 4 BCD digts to 16 binary .

Status
Not open for further replies.

xbop

New Member
I use MIcrochip Pic16F946 for my project. The project has 4 digits for setting up Private ID code so that multiple users can use this controller without interference. I want to write c code to convert this 4 BCD digits to 16 Binary number. Remenber, 1 BCD digit represent 4 bits in binary or 0 to 9 in decimal.

Can anyone, help me write this code, assumeing it is packed BCD.
 
Like this ?

unsigned int bcdNum = 0x1234;
unsigned int value;

value = (bcdNum & 0x000F)*1; // units
bcdNum = bcdNum>>4;
value += (bcdNum & 0x000F)*10; // tens
bcdNum = bcdNum>>4;
...

EDIT: Forgot to shift
 
Last edited:
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…