### Thread: Converts a BCD number into a binary number.

1. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
2
Rep Power
0

#### Converts a BCD number into a binary number.

im making a function for a BCD conversion utility for this

Code:
```//! Converts a BCD number into a binary number.
extern uint8_t CvtBin( uint8_t BcdNumber );```
for a BIG Endian system. Currently I have the code below... can somebody help me shorten the code? thanks..

Code:
```int main()
{
int Num;
int A=0,B=0,C,D,E=1;
int F[100];

printf("Enter the number :");
scanf("%d",&Num);

while(Num>0)
{
A=Num%2;
F[B]=A;
Num=Num/2;
B++;

}

if(B<4)

{
C=B;
for(D=1;D<=4-C;D++)
{
F[B]=0;
B++;
}
}

if(B>4)

{
for(D=0;D<B;D++)
{ if(E==4)
E=1;
else
E++;
}
E--;
if(E<4)

{
for(D=1;D<=4-E;D++)

{
F[B]=0;
B++;
}
}
}

for(E=1,D=B-1;D>=0;D--,E++)
{ printf("%d",F[D]);

if(E==4)
{ printf(" ");
E=0;
}

}

getch();

}

}```
***Please no insult/trolling/sarcastic remarks... i am only here to learn. I'll accept constructive criticism if you supply your insult with a correct answer. Thanks. :)
2. Are you submitting this as an entry toThe International Obfuscated C Code Contest?

Your code needs indentation and variable names other than A-Z. I have not bothered looking at the code, but I just wanted to post that others probably wont look at it either till you make it easy for them.

• mitakeet agrees : Obfuscated indeed!
3. [edit:] executable Iversion notation computes the bit patterns with #: i. 10
Code:
```   #:i.10
0 0 0 0
0 0 0 1
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1
0 1 1 0
0 1 1 1
1 0 0 0
1 0 0 1

NB. verb bcb computes "binary coded base"
bcb=:#:@(#.^:_1)
10 bcb 2345  NB. 2345 in binary coded base 10 digits
0 0 1 0
0 0 1 1
0 1 0 0
0 1 0 1

60 bcb 2345  NB. binary coded base 60 digits.
1 0 0 1 1 1
0 0 0 1 0 1

5+39*60
2345```
[/edit]
Code:
```#include"local.h"/*includes stdio.h and stdlib.h */

int bit_patterns[] = {0,1,2,3,4,5,6,7,8,9};
char*representation[] = {
"0000","0001","0010","0011","0100","0101","0110","0111","1000","1001",
};

int main() {
long long unsigned Num,i;
char*F[100];
printf("Enter finite number less than the minimum of a google and %llu :",~(long long unsigned)0);
if ((1 != scanf("%llu",&Num)) || (! Num))
return fputs("loser",stderr), EXIT_FAILURE;
for (i = 99; Num; --i, Num /= 10)
F[i] = representation[Num%10];
for (++i; i < 100; ++i)
fputs(F[i],stdout),putchar(' ');
putchar('\n');
getchar();
return EXIT_SUCCESS;
}```
Last edited by b49P23TIvg; August 13th, 2012 at 09:11 AM.
4. No Profile Picture
Registered User
Devshed Newbie (0 - 499 posts)

Join Date
Aug 2012
Posts
2
Rep Power
0
Thanks!... you're the only one who has given a clear code and I could actually understand it.. thank you so much ^^.. :)

I have another question if I have packed BCD can I have the same formula you have to convert to Binary? because right now its in decimal right? :)
5. Code:
```/* program stores packed BCD in an array of unsigned char */

/*#include"local.h" contains */
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define DIM(A) (sizeof((A))/sizeof(*(A)))

char
*packedBCD[/*256*/] = { /* ,"1"1 _~(6 4\$'X'),~(#~' '&~:)"1":#:i.10  NB. j code to write these strings */
"0000 0000","0000 0001","0000 0010","0000 0011","0000 0100","0000 0101","0000 0110","0000 0111","0000 1000","0000 1001","0000 XXXX","0000 XXXX","0000 XXXX","0000 XXXX","0000 XXXX","0000 XXXX",
"0001 0000","0001 0001","0001 0010","0001 0011","0001 0100","0001 0101","0001 0110","0001 0111","0001 1000","0001 1001","0001 XXXX","0001 XXXX","0001 XXXX","0001 XXXX","0001 XXXX","0001 XXXX",
"0010 0000","0010 0001","0010 0010","0010 0011","0010 0100","0010 0101","0010 0110","0010 0111","0010 1000","0010 1001","0010 XXXX","0010 XXXX","0010 XXXX","0010 XXXX","0010 XXXX","0010 XXXX",
"0011 0000","0011 0001","0011 0010","0011 0011","0011 0100","0011 0101","0011 0110","0011 0111","0011 1000","0011 1001","0011 XXXX","0011 XXXX","0011 XXXX","0011 XXXX","0011 XXXX","0011 XXXX",
"0100 0000","0100 0001","0100 0010","0100 0011","0100 0100","0100 0101","0100 0110","0100 0111","0100 1000","0100 1001","0100 XXXX","0100 XXXX","0100 XXXX","0100 XXXX","0100 XXXX","0100 XXXX",
"0101 0000","0101 0001","0101 0010","0101 0011","0101 0100","0101 0101","0101 0110","0101 0111","0101 1000","0101 1001","0101 XXXX","0101 XXXX","0101 XXXX","0101 XXXX","0101 XXXX","0101 XXXX",
"0110 0000","0110 0001","0110 0010","0110 0011","0110 0100","0110 0101","0110 0110","0110 0111","0110 1000","0110 1001","0110 XXXX","0110 XXXX","0110 XXXX","0110 XXXX","0110 XXXX","0110 XXXX",
"0111 0000","0111 0001","0111 0010","0111 0011","0111 0100","0111 0101","0111 0110","0111 0111","0111 1000","0111 1001","0111 XXXX","0111 XXXX","0111 XXXX","0111 XXXX","0111 XXXX","0111 XXXX",
"1000 0000","1000 0001","1000 0010","1000 0011","1000 0100","1000 0101","1000 0110","1000 0111","1000 1000","1000 1001","1000 XXXX","1000 XXXX","1000 XXXX","1000 XXXX","1000 XXXX","1000 XXXX",
"1001 0000","1001 0001","1001 0010","1001 0011","1001 0100","1001 0101","1001 0110","1001 0111","1001 1000","1001 1001","1001 XXXX","1001 XXXX","1001 XXXX","1001 XXXX","1001 XXXX","1001 XXXX",
"XXXX 0000","XXXX 0001","XXXX 0010","XXXX 0011","XXXX 0100","XXXX 0101","XXXX 0110","XXXX 0111","XXXX 1000","XXXX 1001","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX",
"XXXX 0000","XXXX 0001","XXXX 0010","XXXX 0011","XXXX 0100","XXXX 0101","XXXX 0110","XXXX 0111","XXXX 1000","XXXX 1001","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX",
"XXXX 0000","XXXX 0001","XXXX 0010","XXXX 0011","XXXX 0100","XXXX 0101","XXXX 0110","XXXX 0111","XXXX 1000","XXXX 1001","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX",
"XXXX 0000","XXXX 0001","XXXX 0010","XXXX 0011","XXXX 0100","XXXX 0101","XXXX 0110","XXXX 0111","XXXX 1000","XXXX 1001","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX",
"XXXX 0000","XXXX 0001","XXXX 0010","XXXX 0011","XXXX 0100","XXXX 0101","XXXX 0110","XXXX 0111","XXXX 1000","XXXX 1001","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX",
"XXXX 0000","XXXX 0001","XXXX 0010","XXXX 0011","XXXX 0100","XXXX 0101","XXXX 0110","XXXX 0111","XXXX 1000","XXXX 1001","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX","XXXX XXXX"
},
*packedDecimalDigits[/*256*/] = { /* ,(16 16#:i.256){'0123456789',6#'X' */
"00","01","02","03","04","05","06","07","08","09","0X","0X","0X","0X","0X","0X","10","11","12","13","14","15","16","17","18","19","1X","1X","1X","1X","1X","1X","20","21","22","23","24","25","26","27","28","29","2X","2X","2X","2X","2X","2X","30","31","32","33","34","35","36","37","38","39","3X","3X","3X","3X","3X","3X","40","41","42","43","44","45","46","47","48","49","4X","4X","4X","4X","4X","4X","50","51","52","53","54","55","56","57","58","59","5X","5X","5X","5X","5X","5X","60","61","62","63","64","65","66","67","68","69","6X","6X","6X","6X","6X","6X","70","71","72","73","74","75","76","77","78","79","7X","7X","7X","7X","7X","7X","80","81","82","83","84","85","86","87","88","89","8X","8X","8X","8X","8X","8X","90","91","92","93","94","95","96","97","98","99","9X","9X","9X","9X","9X","9X","X0","X1","X2","X3","X4","X5","X6","X7","X8","X9","XX","XX","XX","XX","XX","XX","X0","X1","X2","X3","X4","X5","X6","X7","X8","X9","XX","XX","XX","XX","XX","XX","X0","X1","X2","X3","X4","X5","X6","X7","X8","X9","XX","XX","XX","XX","XX","XX","X0","X1","X2","X3","X4","X5","X6","X7","X8","X9","XX","XX","XX","XX","XX","XX","X0","X1","X2","X3","X4","X5","X6","X7","X8","X9","XX","XX","XX","XX","XX","XX","X0","X1","X2","X3","X4","X5","X6","X7","X8","X9","XX","XX","XX","XX","XX","XX"
};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                         /* Lambert Electronics, LLC.  USA, NY. */

unsigned char beatsMeWhatToNameThis[/* 256 */] = { /* 10#.16 16#:i.256 NB. j */
0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,100,101,102,103,104,105,100,101,102,103,104,105,106,107,108,109,110,111,112,113,114,115,110,111,112,113,114,115,116,117,118,119,120,121,122,123,124,125,120,121,122,123,124,125,126,127,128,129,130,131,132,133,134,135,130,131,132,133,134,135,136,137,138,139,140,141,142,143,144,145,140,141,142,143,144,145,146,147,148,149,150,151,152,153,154,155,150,151,152,153,154,155,156,157,158,159,160,161,162,163,164,165 /* 10#.16 16#:i.256 */
};                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           /* Lambert Electronics, LLC.  USA, NY. */

long long unsigned packAsBCD(long long unsigned Num,unsigned char*buffer,size_t bufsize) {
unsigned char twodigits;
memset(buffer,0,bufsize);
if (! bufsize)
return Num;
for (i = bufsize; Num && i--; ) {
twodigits = Num % 100;
buffer[i] = (((twodigits / 10) % 10) << 4) | (twodigits % 10);
Num /= 100;
}
return Num;
}

void displayPackedBCD(unsigned char buffer[],size_t bufsize,FILE*o) {
size_t i,j;
for (i = 0; (i < bufsize) && (! buffer[i]); ++i)
;
if (bufsize && (i == bufsize)) { /* special case for 0 */
fputs("0\n",o);
return;
}
fputs("An X in the output indicates failure\n",o);
for (j = i; j < bufsize; ++j)
fprintf(o,"   %s      ",packedDecimalDigits[(int)(buffer[j])]);
fputc('\n',o);
for (j = i; j < bufsize; ++j)
fputs(packedBCD[(int)(buffer[j])],o), fputs("  ",o);
fputc('\n',o);
}

long long unsigned unpackFromBCD(unsigned char buffer[],size_t bufsize) {
/* no good reason not to use shifting as with the packing */
size_t i;
long long unsigned result;
for (result = i = 0; i < bufsize; ++i)
result = result*100 + beatsMeWhatToNameThis[buffer[i]];
return result;
}

int main() {