+ Post New Thread
Results 1 to 10 of 10
  1. #1
    Member level 4
    Points: 2,549, Level: 11

    Join Date
    Feb 2005
    Posts
    75
    Helped
    3 / 3
    Points
    2,549
    Level
    11

    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?
    please help
    thanks

  2. #2
    Super Moderator
    Points: 37,754, Level: 47
    flatulent's Avatar
    Join Date
    Jul 2002
    Location
    Middle Earth
    Posts
    4,632
    Helped
    474 / 474
    Points
    37,754
    Level
    47

    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



    •   Alt5th February 2005, 04:54

      advertising

        
       

  3. #3
    Junior Member level 3
    Points: 1,696, Level: 9

    Join Date
    Jan 2005
    Posts
    28
    Helped
    4 / 4
    Points
    1,696
    Level
    9

    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



  4. #4
    Advanced Member level 3
    Points: 6,852, Level: 19
    SphinX's Avatar
    Join Date
    Jan 2002
    Location
    EGYPT
    Posts
    822
    Helped
    54 / 54
    Points
    6,852
    Level
    19

    hexadecimal to bcd

    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
    ;
    ;  Comments:
    ;	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
    		ret				; Return To Caller
    		endproc
    SphinX



    •   Alt5th February 2005, 11:12

      advertising

        
       

  5. #5
    Advanced Member level 1
    Points: 6,346, Level: 18

    Join Date
    Nov 2004
    Posts
    444
    Helped
    24 / 24
    Points
    6,346
    Level
    18

    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. #6
    Advanced Member level 3
    Points: 6,852, Level: 19
    SphinX's Avatar
    Join Date
    Jan 2002
    Location
    EGYPT
    Posts
    822
    Helped
    54 / 54
    Points
    6,852
    Level
    19

    convert hex to bcd

    Salam,

    You will find a code here

    http://www.piclist.com/techref/micro...adix/index.htm

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

    Bye



  7. #7
    Member level 1
    Points: 1,666, Level: 9

    Join Date
    Feb 2005
    Posts
    37
    Helped
    1 / 1
    Points
    1,666
    Level
    9

    bcd to hex conversion

    Quote 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. #8
    Newbie level 4
    Points: 751, Level: 6

    Join Date
    May 2009
    Location
    turkey
    Posts
    7
    Helped
    1 / 1
    Points
    751
    Level
    6

    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. #9
    Member level 1
    Points: 1,147, Level: 7

    Join Date
    May 2009
    Posts
    37
    Helped
    0 / 0
    Points
    1,147
    Level
    7

    Re: conversion hex to bcd

    sorry wrong post i got entered in....



  10. #10
    Advanced Member level 1
    Points: 3,969, Level: 14
    ZASto's Avatar
    Join Date
    Jan 2008
    Location
    Messy Corner, 44°47'31.56"N, 20°28'9.16"E
    Posts
    476
    Helped
    94 / 94
    Points
    3,969
    Level
    14

    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
    Make no mistake between my personality and my attitude.
    My personality is who I am.
    My attitude depends on who you are.



+ Post New Thread
Please login