Electronic Circuits and Projects Forum



Convert BCD to Binary

12 Last »
  1. #1
    apricot_star apricot_star is offline

    Convert BCD to Binary

    Hi anyone knows how to Convert BCD to Binary in PIC18F2620 using c language?

  2. #2
    ESchemainda ESchemainda is offline

    Convert BCD to Binary

    I do it by hand.
    Supposing you have an array of BCD numbers.

    int BCD_numbers[] = {3, 2, 7, 6, 8};
    int result, i;

    result = 0;
    for(i=0;i<5;i++)
    result *= 10 + BCD_numbers[i];

    result now should contain 0x7FFF.
    -1

  3. #3
    Pommie Pommie is offline
    Assuming it's packed BCD in an integer then the following should work,
    Code :
    unsigned int BCDtoI(unsigned int BCD){
    unsigned int result;
        result=(BCD>>12)*1000;
        result+=((BCD>>8)&0x0f)*100;
        result+=((BCD>>4)&0x0f)*10;
        result+=((BCD)&0x0f);
        return result;
    }

    Mike.
    0

  4. Thread Starter #4
    apricot_star apricot_star is offline
    is it okie to write in this way:

    int BCD2BINARY(int x)
    {
    int binary;
    binary = ((x & 0xF0 )>>4 ) * 10 + (x & 0x0F);
    return (binary);
    }
    0

  5. #5
    Pommie Pommie is offline
    Doing it that way is fine as long as your number is only 2 digits.

    Edit, if it's only a 2 digit number then you can do,
    bcd-=((bcd&0xf0)>>3)*3;

    Mike.
    0
    Last edited by Pommie; 28th August 2008 at 08:37 AM.

  6. #6
    poppy2008 poppy2008 is offline
    Quote Originally Posted by Pommie View Post
    Assuming it's packed BCD in an integer then the following should work,
    Code :
    unsigned int BCDtoI(unsigned int BCD){
    unsigned int result;
        result=(BCD>>12)*1000;
        result+=((BCD>>8)&0x0f)*100;
        result+=((BCD>>4)&0x0f)*10;
        result+=((BCD)&0x0f);
        return result;
    }

    Mike.
    hi pommie can u explain ur coding,i am new in c,but i am very much interested to learn,please
    0

  7. #7
    Pommie Pommie is offline
    All the code is doing is getting the relevant digit and multiplying by the decimal value. So, BCD>>12 will get the thousands digit which is then multiplied by 1000.

    Mike.
    0

  8. Thread Starter #8
    apricot_star apricot_star is offline
    Hi pommie,

    Thanks for the reply, however i was wondering why must it shift by 3 and times 3?
    0

  9. #9
    Pommie Pommie is offline
    Quote Originally Posted by apricot_star View Post
    Hi pommie,

    Thanks for the reply, however i was wondering why must it shift by 3 and times 3?
    The way to convert a BCD number to it's binary equivalent is to divide the high digit by 16 and then multiply by 10. Another way to do the same thing is to subtract the high digit * 6 (16-10). To do this in code you do BCD>>4 to get the high digit and then multiply by 6. As 6 is 3*2 then you can reduce the number of shifts by 1 and multiply by 3. This is just the same as saying that BCD/16*6 is the same as BCD/8*3.

    The less efficient (but more logical) way to write the same thing is,
    bcd-=((bcd&0xf0)>>4)*6;

    Mike.
    0

  10. #10
    vietbk vietbk is offline
    Convert BCD to Binary
    Hi anyone knows how to Convert BCD to Binary in Psoc using assembly language?
    0

12 Last »
Tags
Similar Threads
Thread Starter Forum Replies Last Post
Convert binary to BCD code genetica General Electronics Chat 8 26th July 2010, 05:44 PM
how to convert rs 232 tx/rx to binary elmediel Robotics & Mechatronics 5 12th January 2008, 06:28 AM
binary to bcd jimbob jones General Electronics Chat 6 9th November 2005, 06:57 PM
Trying to convert binary to ascii ChriX Microcontrollers 9 7th May 2005, 12:30 PM
Algorithm to convert 64 bit Binary to BCD ljcox Microcontrollers 6 21st November 2004, 03:34 AM
Electronic Circuits  |  Learning Electronics

Join our community with over 100,000 Members! It's free, easy and when you're logged in you have many more features! Click to register.
Page Time: 0.12420 seconds      Memory: 7,657 KB      Queries: 16      Templates: 0