Created
October 13, 2017 00:18
-
-
Save gatherKnowledge/17899417cca304af4c0b29c3b1c0d560 to your computer and use it in GitHub Desktop.
integer to bcd
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Show Integer To BCD-code | |
* - Tokkenizer function makes input array with 10 below Integer. | |
* - toBin function shows binary code. | |
*/ | |
// | |
void tokkenizer(int n){ | |
int i ; | |
int arr[20] = {0,} ; | |
char r[20] = {0,} ; | |
i = 0 ; | |
if( n == 0 ){ | |
printf("0000") ; | |
return ; | |
} | |
while ( n/10 != 0 || n % 10 != 0) | |
{ | |
arr[i] = n % 10 ; | |
n = n / 10 ; | |
i ++ ; | |
} | |
n = i ; | |
while( i >= 0 ) | |
{ | |
r[n-i] = arr[i-1] ; | |
i-- ; | |
} | |
for(int j = 0 ; j < n ; j++ ) | |
{ | |
toBin((int)r[j]) ; | |
if ( j != n - 1 ){ | |
printf("_") ; | |
} | |
} | |
} | |
// below 10 input show binary --> 0000 | |
void toBin(int n){ | |
int i ; | |
int arr[4] = {0,} ; | |
char r[4] = {0,} ; | |
i = 0 ; | |
while ( n/2 != 0 || n % 2 != 0) | |
{ | |
arr[i] = n % 2 ; | |
n = n / 2 ; | |
i ++ ; | |
} | |
i = 3 ; | |
while( i >= 0 ) | |
{ | |
r[3-i] = arr[i] ; | |
i-- ; | |
} | |
for(int j = 0 ; j < 4 ; j++ ) | |
{ | |
printf("%d", r[j]) ; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment