# conversion hex to bcd

1. ## hex to bcd

hi people
can anybody help me out for how to convert a hexadecimal number into BCD format.for eg: i have a counter having count 504 .in hex it is 1F8h .now what will be its BCD code?
thanks

•

2. ## bcd to hex

If 504 is the decimal, convert the 5 and the 0 and the 4 to four bit binary numbers and string them together. 0101 0000 0100 is the answer. Or without spaces 010100000100

3. ## hex to bcd conversion

hi

BCD code of every number is completely like decimal and does't depend
on hex fomat, it means that when you have the number 504 in decimal, it's BCD code has 4 bit for digit 4, 4 bit for digit 0 and 4 bit for the digit 5 ( and BCD has no digit more than 9), so in every 8 bit register, you have 2 BCD numbers, so your number will be
0000'0101 0000'0100

masoud

Salam,

Hex2BCD Converter using 8051

Code:
```;
;****************************************************************************
;
;  Description:
;	Convert Value In Acc From Hex To BCD.
;
;  Entry Requirements:
;	Acc Has Value In Binary To Convert To BCD
;
;  On Exit:
;	Acc Has Entry Value Converted To BCD
;
;  Affected:
;	PSW.CY, PSW.Z, PSW.P, Acc
;
;  Stack:
;	1 Bytes, Not Including Space Used By Called Routines
;
;	Values Greater Than 99 Will Not Work Properly.
;
;****************************************************************************

UTIL_BINTOBCD	proc
push	b			; Save B
mov	b,#10			; Divide By 10
div	ab			; Do Divide
swap	a			; Move Result To High Of A
orl	a,b			; OR In Remainder
pop	b			; Recover B
endproc```
SphinX

•

5. ## hex to bcd converter

thanks for replying but if i have a 16bit value or a 32 bit value in that case how will the code be written with 8051 microcontroller. cause the value in the programming will be hexadecimal .suppose the value in hex is 2ED(h) then how to convert this to BCD form.

6. ## convert hex to bcd

Salam,

You will find a code here

in PIC but you can convert it to 8051 becuase it use simple Bitwise instructions

Bye

7. ## bcd to hex conversion

Originally Posted by garg29
suppose the value in hex is 2ED(h) then how to convert this to BCD form.
It would be much easier if you first convert it to decimal format and then write the binary code for each digfit of the number e..g. for 2ED(h), the decimal equivalent would be 749 and the resulting BCD code would be 0111 0100 1001.

8. ## Re: conversion hex to bcd

in that program 16 bit hex convert to bcd. I dont know the algorithm exactly but the result is right every time.if you have any problem you can ask me...

1 members found this post helpful.

9. ## Re: conversion hex to bcd

sorry wrong post i got entered in....

•

10. ## conversion hex to bcd

It seems that lot of people have problem with BCD.

BCD stands for Binary Coded Decimal. As we all know decimal numbers have range from 0 to 9, thanslated to binary it is 0b0000 to 0b1001. As the decimal digit occupies no more than 4 bits any 8 bit variable (or register) can contain TWO decimal digits in it's two 4-bit chunks called nibbles.

If you take some datasheets for RTC chips, that's the way of keeping Hours, Minutes, Secinds in only 3 bytes.

HTH

--[[ ]]--