How to convert binary to BCD ?
How to convert binary to BCD ?
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.
Please anyone help me.
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.
Please anyone help me.
Re: How to convert binary to BCD ?
BCD to Integer
Integer to BCD
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;
}
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;
}
Re: How to convert binary to BCD ?
Thank you Saman...
Now I can write a new function for binary to BCD.
75% is your.
Am I correct ?
I cant understand Saman's while loop Please,If you can explain it if i=22
If i=22
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)
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;
}
I cant understand Saman's while loop 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
}
or
another good system like you..(I decided this for your concession)
Re: How to convert binary to BCD ?
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
*Just thought to reply. It may be wrong
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
*Just thought to reply. It may be wrong
Re: How to convert binary to BCD ?
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 )
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 )
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: How to convert binary to BCD ?
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.
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.
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);\
}
they never contributed any code to any large project. It's impossible to live without preprocessor.
Re: How to convert binary to BCD ?
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.
This function return BCD as an integer..
I think this is stupid problem..
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;
}
I think this is stupid problem..
Re: How to convert binary to BCD ?
BCD is about representing each digit of a decimal number in a nibble (4-bits).I think this is stupid problem..
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.
- SemiconductorCat
- Major
- Posts: 455
- Joined: Mon Aug 22, 2011 8:42 pm
- Location: currently in hyperspace
Re: How to convert binary to BCD ?
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.
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.
Re: How to convert binary to BCD ?
Thanks Saman and SemiconductorCat .... Now It's clear ...
REP+
If integer is 22,
BCD is,
2 - 0010
2 - 0010
0010 0010
When this number is returned as an integer, it is 34 ..
Am I correct?
OK
Now I have another problem in Saman's last reply.
Is Reason for limit ,for 32 bit output?
REP+
If integer is 22,
BCD is,
2 - 0010
2 - 0010
0010 0010
When this number is returned as an integer, it is 34 ..
Am I correct?
OK
Now I have another problem in Saman's last reply.
Why 26 bit ? Cant represent more than 26 bit numbers ?A 26-bit number can be represented with 4 bytes.
226 - 1 = 67108863
Is Reason for limit ,for 32 bit output?