---
+ Post New Thread
Results 1 to 6 of 6
  1. #1
    Member level 5
    Points: 1,894, Level: 10

    Join Date
    Nov 2005
    Location
    India
    Posts
    81
    Helped
    3 / 3
    Points
    1,894
    Level
    10

    hex to bcd

    how to convert a hex value to bcd using keil c

    •   Alt12th March 2006, 03:27

      advertising

        
       

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

    hex to bcd conversion in c

    Salam,

    You didn't mention what is the length of the hex 8 bit, 16 bits ...etc

    Ok here is a simple method to convert hex (8bit) to bcd

    One restriction that method is vaild fot numbers 0 - 99 only.

    for example if you want to convert 99 to bcd

    1- divide 99 by 10
    2- put the result in the high nibble of the result variable
    3- put the reminder in the low nibble of the result variable

    This method is very easy to implement in C like this

    Code:
    unsigned char hex2bcd (unsigned char x)
    {
        unsigned char y;
        y = (x / 10) << 4;
        y = y | (x % 10);
        return (y);
    }
    Where X is the Hex , and Y is the result

    Enjoy

    Bye


    1 members found this post helpful.

    •   Alt12th March 2006, 07:52

      advertising

        
       

  3. #3
    Advanced Member level 3
    Points: 12,871, Level: 27
    silvio's Avatar
    Join Date
    Dec 2001
    Location
    Romania
    Posts
    716
    Helped
    110 / 110
    Points
    12,871
    Level
    27

    hex to bcd conversion c code

    From fmayr@electronic.tu-graz.ac.at Thu Feb 6 10:24:30 1997
    Date: Wed, 05 Feb 1997 15:36:00 +0100
    From: Friedrich Mayr <fmayr@electronic.tu-graz.ac.at>
    To: 8051code@keil.com
    Subject: Fast LONG to BCD conversion

    This function was written within the Keil C51 environment
    by Dr. Roehrer (Inst. f. Elektronik, TU-Graz) and me
    to be called from a C - Program as: "void bin2bcd(void)"

    The following global variable (must be located in the
    internal RAM of the processor, "data" or "idata" ) is
    used both to pass the argument (a "long" value) to the
    function, as well as to receive the result (6 digits):

    union
    {
    unsigned char buf[6];
    long in;
    } idata U_b;

    The purpose of the code is to convert a signed long value
    to BCD - code with leading sign if needed. Values exceeding
    the range of 6(-:5) Digits should all be displayed by '-'.
    The usage of memory was critical because no external RAM is
    present in the system, so only the Accu, B, R1, R2, R3, R4
    are used aside of the digit- (and argument-) buffer "U_b".

    The trick applied in the function is to use a
    "small" (nibble by nibble) division and the extensive
    use of the '51 - opcodes XCHD and SWAP.
    This results in very fast execution.
    The function can easily be modified for example
    to time-calculations or extended to the full "long"
    range .
    http://www.boerde.de/~matthias/m8051/bin2bcd.zip



    •   Alt12th March 2006, 07:57

      advertising

        
       

  4. #4
    Junior Member level 3
    Points: 1,673, Level: 9
    tohbas's Avatar
    Join Date
    Mar 2005
    Location
    Thailand
    Posts
    26
    Helped
    1 / 1
    Points
    1,673
    Level
    9

    hex to bcd in c

    float a;
    unsigned char b;


    b = 0xff;
    a = b;



  5. #5
    Newbie level 1
    Points: 531, Level: 4

    Join Date
    Dec 2009
    Location
    bangalore
    Posts
    1
    Helped
    0 / 0
    Points
    531
    Level
    4

    Re: convert bcd to hex in c

    // pass input 8 bit BCD format and conversion length, and get back hex values
    // in output buffer.
    void Bcd_To_Hex(unsigned char *Input_Buff, unsigned char *Output_Buff,unsigned char Len)
    { // by Neeraj Verma : neeraj.niet@gmail.com
    unsigned char i,j;
    for(i=0,j=0;i<Len;i++,j++)
    { if((i+1) >= Len)
    {
    if(*(Input_Buff+Len-1-i) >0x40)
    *(Output_Buff+j) = (*(Input_Buff+Len-1-i)-0x37);
    else
    *(Output_Buff+j) = (*(Input_Buff+Len-1-i)-0x30);
    }
    else
    {
    if(*(Input_Buff+Len-1-i) >0x40)
    { if(*(Input_Buff+Len-2-i) >0x40)
    {
    *(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x37)<<4) | (*(Input_Buff+Len-1-i)-0x37);
    }
    else
    {
    *(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x30)<<4) | (*(Input_Buff+Len-1-i)-0x37);
    }
    }
    else
    { if(*(Input_Buff+Len-2-i) >0x40)
    {
    *(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x37)<<4) | (*(Input_Buff+Len-1-i)-0x30);
    }
    else
    {
    *(Output_Buff+j) = ((*(Input_Buff+Len-i-2)-0x30)<<4) | (*(Input_Buff+Len-1-i)-0x30);
    }
    }
    }
    i++;
    }
    }



  6. #6
    Junior Member level 2
    Points: 616, Level: 5

    Join Date
    Dec 2009
    Location
    Bangalore
    Posts
    24
    Helped
    2 / 2
    Points
    616
    Level
    5

    Re: convert hex to bcd in c

    /* Prototype */
    void Hex2Decimal(unsigned long num);

    /* Hex Variable in any register */
    unsigned long num = 0x499602D2 ; /* 1234567890 */

    int main (void)
    {
    Hex2Decimal(num); /* Convert given hex to decimal digits */

    return 1; /* Good C!, not required */
    }

    /* Function */

    /* Converts an unsigned hex number availabe in the micro registers to decimal digits
    and stores the decimal digits in an array */
    void Hex2Decimal(unsigned long num)
    {
    unsigned char value[10];
    unsigned char i = 0;

    while(num > 0)
    {
    value[i] = num % 10;
    num /= 10;
    i = i + 1;
    }
    }



+ Post New Thread
Please login

LinkBacks (?)