Last active
August 29, 2015 14:01
-
-
Save mr-fool/f93112a3e84ec65d4bde to your computer and use it in GitHub Desktop.
Number System Conversion in C
This file contains hidden or 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
| #include <stdio.h> | |
| #include <math.h> | |
| #include <stdlib.h> | |
| /*Declaring Constant */ | |
| int const BASE = 2; | |
| int const BASE_TEN = 10; | |
| /* converting from decimal require the following information input number,quotient, remainder*/ | |
| int inputNumber; | |
| int calculationRemainder; | |
| int lengthForArray; | |
| int lengthForDecimal; | |
| int quotient; | |
| int result = 0; | |
| int exponent = 0; | |
| /*Function Prototype*/ | |
| int length(); | |
| int calculation(); | |
| /*Calcuating the length of the input*/ | |
| int length() { | |
| /*Calculating the length for the array*/ | |
| lengthForArray = floor((log(inputNumber) / log(BASE_TEN)) + 1); | |
| printf("%s%d\n","Legnth of the input number ",lengthForArray); | |
| return 0; | |
| } | |
| /*Actual calucation place*/ | |
| int calculation() { | |
| /*printf("%s%d\n","The precondition result is: ",result);*/ | |
| quotient = inputNumber; | |
| while (quotient != 0) { | |
| calculationRemainder = quotient % 10; /*Finding the remainder */ | |
| quotient = quotient / 10; | |
| result = result + calculationRemainder * pow(2,exponent); | |
| ++exponent; | |
| /*printf("%s%d\n","The initial result is: ",result);*/ | |
| } | |
| printf("%s%d\n","The result is: ",result); /*priting the final result*/ | |
| return 0; | |
| } | |
| int main(int arc, char *argv[]) { | |
| inputNumber = atoi( argv[1]); | |
| length(); | |
| calculation(); | |
| return 0; | |
| } |
This file contains hidden or 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
| #include <stdlib.h> | |
| #include <string.h> | |
| #include <stdio.h> | |
| /*Declaring variables*/ | |
| int index_one = 0; | |
| int index_two = 0; | |
| int decimal; | |
| char inputNumber[9001]; | |
| char result[9001]; | |
| /* */ | |
| /*Function Prototype*/ | |
| void calculation(); | |
| void printResult(); | |
| /*Convertion using ASCII table*/ | |
| void calculation() | |
| { | |
| /*Ensuring that index_one start from 0*/ | |
| while (inputNumber[index_one]) { | |
| inputNumber[index_one] = inputNumber[index_one]-'0'; | |
| index_one++; | |
| } | |
| --index_one; | |
| while( index_one - 2 >= 0) { | |
| /*Converting from binary number to decimal*/ | |
| decimal = inputNumber[index_one-3]*8+inputNumber[ index_one - 2] *4 + inputNumber[index_one-1]*2+inputNumber[index_one]; | |
| /*if the number is between 10-15 convert it to A-F | |
| * Could have been easier if the prof would allow the use of %X*/ | |
| if (decimal > 9){ | |
| result[index_two++] = decimal + '7'; | |
| } | |
| else { | |
| result[index_two++] = decimal + '0'; | |
| } | |
| index_one = index_one - 4; | |
| } | |
| /*Mapping ascii value */ | |
| if (index_one == 1) { | |
| result[index_two] = inputNumber[index_one-1]*2 + inputNumber[index_one]+'0'; | |
| } | |
| /*More ascii value mapping*/ | |
| else if (index_one == 0) { | |
| result[index_two] = inputNumber[index_one]+'0'; | |
| } | |
| /*hexadecimal value */ | |
| else{ | |
| index_two--; | |
| } | |
| } | |
| /*index_twoust output the result.*/ | |
| void printResult() { | |
| while (index_two>=0) { | |
| printf("%c", result[index_two--]); | |
| } | |
| printf("\n"); | |
| } | |
| int main(int argc, char *argv[]) | |
| { | |
| sscanf(argv[1],"%s",inputNumber); | |
| calculation(); | |
| printResult(); | |
| return 0; | |
| } |
This file contains hidden or 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
| #include <stdio.h> | |
| #include <math.h> | |
| #include <stdlib.h> | |
| /*Declaring Constant */ | |
| int const BASE = 2; | |
| int const BASE_TEN = 10; | |
| /* converting from decimal require the following information number, quotient, remainder*/ | |
| int inputNumber; | |
| int quotient; | |
| int calculationRemainder; | |
| int lengthForArray; | |
| int lengthForDecimal; | |
| /*Function Prototype*/ | |
| int length(); | |
| int calculation(); | |
| /*Calcuating the length of the input*/ | |
| int length() { | |
| /*Calculating the length for the array*/ | |
| lengthForArray = floor((log(inputNumber) / log(BASE)) + 1); | |
| printf("%s%d\n","Legnth of the number in binary ",lengthForArray); | |
| /*Calculating the legth in decimal form */ | |
| lengthForDecimal = floor((log(inputNumber) / log(BASE_TEN)) + 1); | |
| printf("%s%d\n","Legnth of the number input ",lengthForDecimal); | |
| return 0; | |
| } | |
| /*Actual calucation place*/ | |
| int calculation() { | |
| int location = 0; /*Used to keep track of the array*/ | |
| int resultArray[lengthForArray],/*Setting an array size*/ | |
| quotient = inputNumber; | |
| while (quotient != 0) { | |
| resultArray[location++] = quotient % 2; /* Any odd number mod 2 is always 1 | |
| and any even numver mod 2 is always 0*/ | |
| quotient = quotient / 2; /* since quotient is integer the decimal part will always be dropped :P*/ | |
| } | |
| printf ("%s\n","The binary result is:"); | |
| /*Printing the the array aka the binary number*/ | |
| for (location = location - 1; location >= 0; location--) { | |
| printf("%d", resultArray[location]); | |
| } | |
| printf ("\n"); | |
| return 0; | |
| } | |
| int main(int arc, char *argv[]) { | |
| inputNumber = atoi( argv[1]); | |
| length(); | |
| calculation(); | |
| return 0; | |
| } |
This file contains hidden or 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
| allFiles: bin2dec.c bin2hex.c dec2bin.c hex2bin.c | |
| gcc -Wall bin2dec.c -o bin2dec.out -lm | |
| gcc -Wall bin2hex.c -o bin2hex.out -lm | |
| gcc -Wall dec2bin.c -o dec2bin.out -lm | |
| gcc -Wall hex2bin.c -o hex2bin.out -lm | |
| clean: | |
| rm *.o bin2dec.out bin2hex.out dec2bin.out hex2bin.out |
This file contains hidden or 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
| #include <stdio.h> | |
| #include <stdlib.h> | |
| /*Declaring Arrays*/ | |
| char binaryNumber[9001]; | |
| char hexaDecimal[9001]; | |
| int iteratorControl = 0; | |
| /*Function Prototype*/ | |
| void patternMatching(); | |
| void patternMatching() { | |
| printf("Equivalent binary value: "); | |
| while(hexaDecimal[iteratorControl]){ /* Won't loop once it hits the Null*/ | |
| /*Matching patten*/ | |
| switch(hexaDecimal[iteratorControl]){ | |
| case '0': | |
| printf("0000"); | |
| break; | |
| case '1': | |
| printf("0001"); | |
| break; | |
| case '2': | |
| printf("0010"); | |
| break; | |
| case '3': | |
| printf("0011"); | |
| break; | |
| case '4': | |
| printf("0100"); | |
| break; | |
| case '5': | |
| printf("0101"); | |
| break; | |
| case '6': | |
| printf("0110"); | |
| break; | |
| case '7': | |
| printf("0111"); | |
| break; | |
| case '8': | |
| printf("1000"); | |
| break; | |
| case '9': | |
| printf("1001"); | |
| break; | |
| case 'A': | |
| printf("1010"); | |
| break; | |
| case 'B': | |
| printf("1011"); | |
| break; | |
| case 'C': | |
| printf("1100"); | |
| break; | |
| case 'D': | |
| printf("1101"); | |
| break; | |
| case 'E': | |
| printf("1110"); | |
| break; | |
| case 'F': | |
| printf("1111"); | |
| break; | |
| case 'a': | |
| printf("1010"); | |
| break; | |
| case 'b': | |
| printf("1011"); | |
| break; | |
| case 'c': | |
| printf("1100"); | |
| break; | |
| case 'd': | |
| printf("1101"); | |
| break; | |
| case 'e': | |
| printf("1110"); | |
| break; | |
| case 'f': | |
| printf("1111"); | |
| break; | |
| default: | |
| printf("Invalid base provided"); | |
| exit(EXIT_FAILURE); | |
| } | |
| iteratorControl++; | |
| } | |
| printf("\n"); | |
| } | |
| int main(int argc, char *argv[]) { | |
| sscanf(argv[1],"%s",hexaDecimal); | |
| patternMatching(); | |
| return 0; | |
| } | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment