How to convert binary to BCD ?

C, C++, Visual C++, C++.Net Topics
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

How to convert binary to BCD ?

Post by Nandika » Sun Jan 20, 2013 11:06 pm

Hi Friends, :)

I wan to write a function using C for convert 8bit dip switch values to BCD.
As I think dip switch value is binary.
I coded some function.But,It's binary to decimal. :(
I tried with "itoa" function.No valuable result. :cry:

Please anyone help me.
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to convert binary to BCD ?

Post by Saman » Sun Jan 20, 2013 11:52 pm

BCD to Integer

Code: Select all

unsigned int bcd2i(unsigned int bcd) {
    unsigned int decimalMultiplier = 1;
    unsigned int digit;
    unsigned int i = 0;
    while (bcd > 0) {
        digit = bcd & 0xF;
        i += digit * decimalMultiplier;
        decimalMultiplier *= 10;
        bcd >>= 4;
    }
    return i;
}
Integer to BCD

Code: Select all

unsigned int i2bcd(unsigned int i) {
    unsigned int binaryShift = 1;
    unsigned int digit;
    unsigned int bcd = 0;
    while (i > 0) {
        digit = i % 10;
        bcd += (digit << binaryShift);
        binaryShift += 4;
        i /= 10;
    }
    return bcd;
}
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How to convert binary to BCD ?

Post by Nandika » Mon Jan 21, 2013 6:02 am

Thank you Saman...
:D
Now I can write a new function for binary to BCD.
75% is your. ;)

Code: Select all

unsigned int i2bcd(unsigned int bits[]) {
unsigned int i=0;
unsigned int pos_val=0;
unsigned int dec=0;
unsigned int binaryShift = 1;
unsigned int digit;
unsigned int bcd = 0;

for(i=0;i<7;i++)//i have 8bit binary
{
       pos_val=bits[i];//get Nth bit(0 or 1)
       dec+=pow(2,i)*pos_val;//calculate Nth decimal value and add to total

}
 //Saman's part

    while (dec > 0) {
        digit = dec % 10;
        bcd += (digit << binaryShift);
        binaryShift += 4;
        dec /= 10;
    }
    return bcd;
}
Am I correct ?
I cant understand Saman's while loop :roll: Please,If you can explain it if i=22

If i=22

Code: Select all

while (i > 0) { 
        digit = i % 10;//1
        bcd += (digit << binaryShift);//2
        binaryShift += 4;//3
        i /= 10;//4
    }
At 1,2,3 and 4 , please write digit,bcd and biaryShift values
or
another good system like you..(I decided this for your concession)
User avatar
Rksk
Major
Major
Posts: 730
Joined: Thu Jan 07, 2010 4:19 pm
Location: Rathnapura, Sri Lanka

Re: How to convert binary to BCD ?

Post by Rksk » Mon Jan 21, 2013 6:03 pm

I think that, binaryShift should start from 0.

i = 22
binaryShift = 0

while (i > 0) {
digit = i % 10;//1
bcd += (digit << binaryShift);//2
binaryShift += 4;//3
i /= 10;//4
}

1st cycle
1 digit = 22 % 10 = 2
2 bcd = 0 + (2 << 0) = 2
3 binaryShift = 0 + 4 = 4
4 i = 22/10 = 2

2nd cycle
1 digit = 2 % 10 = 2
2 bcd= 2 + (2 << 4) = 2 + 32 = 34
3 binaryShift = 4 + 4 = 8
4 i = 2/10 = 0

So result is 34 :biggrin:

*Just thought to reply. It may be wrong :D
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How to convert binary to BCD ?

Post by Nandika » Mon Jan 21, 2013 9:03 pm

Hello,
No problem in Rksk explanation..I soundly understood it. :)
Shifting system was the problem.
Now,It,s clear.
Thank you Rksk.

Now I have big question.It originated from Saman's Integer to BCD function.
I want to convert Binary to BCD....
according to Saman's function,First I convert binary to integer and second it convert to BCD using Saman's while loop.

But,
Must be integer 22 is 0010 0010 BCD ? (According to Wikipedia)
Saman's while loop return integer.
Therefore,
Has any system for represent BCD using integer?(like 0010 0010 BCD --> 34 integer :roll: :geek: )
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: How to convert binary to BCD ?

Post by SemiconductorCat » Thu Jan 24, 2013 2:18 am

Binary coded decimal means decimal number have been coded into a nibble.
Nibble means a 4 bits.

Now what? You will say how could I randomly access nibbles like I access bytes in memory.
Oky, that's why C++/C have a preprocessor. You may simply write some simple macros like bellow.

Code: Select all

#define GET_BCD_VALUE(position,pointer) \
  { char* __get_bcd_value_pointer;\
    __get_bcd_value_pointer = (char*)(pointer) +(position)/2; \
  (( position)%2 ) ? (( *__get_bcd_value_pointer )<<4)>>4 : (*__get_bcd_value_pointer)>>4 ;\
  }
#define SET_BCD_VALUE(position,pointer,value) \
  { char* _set_bcd_value_pointer );\
   __set_bcd_value_pointer = (char*) (pointer) +(position)/2 ; \
  ((position)%2) ? ((*__set_bcd_value_pointer) =|(value)<<4 ):(*__set_bcd_value_pointer) =| (value);\
  }

In Java seriously I don't have a clue how to do this. For the people who to remove preprocessor from C/C++
they never contributed any code to any large project. It's impossible to live without preprocessor.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How to convert binary to BCD ?

Post by Nandika » Thu Jan 24, 2013 5:54 pm

Hi SemiconductorCat,
I can't understand your coding. :( It's very sophisticated. :)

My problem is ,How to represent a BCD as integer type ?
this function do it.

Code: Select all

unsigned int i2bcd(unsigned int i) {
    unsigned int binaryShift = 1;
    unsigned int digit;
    unsigned int bcd = 0;
    while (i > 0) {
        digit = i % 10;
        bcd += (digit << binaryShift);
        binaryShift += 4;
        i /= 10;
    }
    return bcd;
}
This function return BCD as an integer..

I think this is stupid problem.. :shock:
User avatar
Saman
Lieutenant Colonel
Lieutenant Colonel
Posts: 828
Joined: Fri Jul 31, 2009 10:32 pm
Location: Mount Lavinia

Re: How to convert binary to BCD ?

Post by Saman » Thu Jan 24, 2013 8:30 pm

I think this is stupid problem..
BCD is about representing each digit of a decimal number in a nibble (4-bits).

A 26-bit number can be represented with 4 bytes.
226 - 1 = 67108863

This has 8 digits. If I represent this in BCD,

3 - 0011
6 - 0110
8 - 1000
8 - 1000
0 - 0000
1 - 0001
7 - 0111
6 - 0110

0110 0111 0001 0000 1000 1000 0110 0011

When this number is returned as an integer, it is 1729136739.
User avatar
SemiconductorCat
Major
Major
Posts: 455
Joined: Mon Aug 22, 2011 8:42 pm
Location: currently in hyperspace

Re: How to convert binary to BCD ?

Post by SemiconductorCat » Thu Jan 24, 2013 9:36 pm

I've revive your function, There's no wrong with it.
It's not stupid.


And I don't feel it's stupid. If the code is neat and clear, I don't worry about it more.

And if you can't say whether it's sophisticated or not before you review it. If you don't understand it at the
first time you look then take a pencil and paper and do a dry run by substituting a dummy value input.
User avatar
Nandika
Captain
Captain
Posts: 247
Joined: Sat Oct 15, 2011 11:40 am
Location: Galle-Sri Lanka

Re: How to convert binary to BCD ?

Post by Nandika » Fri Jan 25, 2013 9:51 pm

Thanks Saman and SemiconductorCat .... Now It's clear ... :D
REP+

If integer is 22,
BCD is,
2 - 0010
2 - 0010

0010 0010

When this number is returned as an integer, it is 34 .. :clap:
Am I correct?

OK
Now I have another problem in Saman's last reply. :?:
A 26-bit number can be represented with 4 bytes.
226 - 1 = 67108863
Why 26 bit ? Cant represent more than 26 bit numbers ?
Is Reason for limit ,for 32 bit output? :idea:
Post Reply

Return to “C/C++ Programming”