Skip to content

Instantly share code, notes, and snippets.

@IMSoley
Last active June 9, 2019 08:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IMSoley/621c15b10480021d8eae1009a01da36c to your computer and use it in GitHub Desktop.
Save IMSoley/621c15b10480021d8eae1009a01da36c to your computer and use it in GitHub Desktop.
Number System Conversion In C (Assignment)
#include <stdio.h>
#include <string.h>
int from, number, to;
long long binNumber;
char hexNum[100], binNum[100];
char options[5][100] = {" ", "Decimal", "Hexadecimal", "Octal", "Binary"};
void selectOption() {
for (int i=1; i<5; i++) {
printf("%d. %s \n", i, options[i]);
}
}
void errorMessage() {
printf("Something is wrong :( !!!");
}
int hexadecimalToBinary(char hexVal[]) {
char hexDigitToBinary[16][100] = {"0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111"};
char hexDigits[16] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
int i = 0, j;
for(i=0; hexVal[i] != '\0'; i++) {
for(j=0; j<16; j++){
if(hexDigits[j] == hexVal[i]){
strcat(binNum, hexDigitToBinary[j]);
}
}
}
return atoi(binNum);
}
long int binaryToOctal(int binarynum) {
long int octalnum = 0, j = 1, remainder;
while (binarynum != 0) {
remainder = binarynum % 10;
octalnum = octalnum + remainder * j;
j = j * 2;
binarynum = binarynum / 10;
}
return octalnum;
}
void binaryToHexadecimal(long long binary) {
int hexConstant[] = {0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111};
long long tempBinary;
char hex[20];
int index, i, digit;
tempBinary = binary;
index = 0;
while(tempBinary!=0) {
digit = tempBinary % 10000;
for(i=0; i<16; i++) {
if(hexConstant[i] == digit) {
if(i<10) {
hex[index] = (char)(i + 48);
} else {
hex[index] = (char)((i-10) + 65);
}
index++;
break;
}
}
tempBinary /= 10000;
}
hex[index] = '\0';
strrev(hex);
printf("%s", hex);
}
long long octalToBinary(int number) {
int octaVal[] = {0, 1, 10, 11, 100, 101, 110, 111};
long long binary=0, place=1;
int rem;
while(number > 0) {
rem = number % 10;
binary = (octaVal[rem] * place) + binary;
number /= 10;
place *= 1000;
}
return binary;
}
void decimalTo(int number, int to) {
int binaryNum[32], octalNum[100], i = 0;
char hexaDeciNum[100];
switch (to) {
case 1:
printf("Gotchya, %d\n", number);
break;
case 2:
while (number != 0) {
int temp = 0;
temp = number % 16;
if(temp < 10) {
hexaDeciNum[i] = temp + 48;
i++;
} else {
hexaDeciNum[i] = temp + 55;
i++;
}
number /= 16;
}
for(int j=i-1; j>=0; j--)
printf("%c", hexaDeciNum[j]);
break;
case 3:
while (number != 0) {
octalNum[i] = number % 8;
number /= 8;
i++;
}
for (int j = i-1; j >= 0; j--)
printf("%d", octalNum[j]);
break;
case 4:
while (number > 0) {
binaryNum[i] = number % 2;
number /= 2;
i++;
}
for (int j = i-1; j >= 0; j--)
printf("%d", binaryNum[j]);
break;
default:
errorMessage();
}
}
void hexadecimalTo(char hexNum[], int to) {
int len = strlen(hexNum), base = 1, dec_val = 0, x;
switch (to) {
case 1:
for (int i=len-1; i>=0; i--) {
if (hexNum[i]>='0' && hexNum[i]<='9') {
dec_val += (hexNum[i] - 48)*base;
base = base * 16;
} else if (hexNum[i]>='A' && hexNum[i]<='F') {
dec_val += (hexNum[i] - 55)*base;
base = base*16;
}
}
printf("%d", dec_val);
break;
case 2:
printf("Gotchya, %s\n", hexNum);
break;
case 3:
x = hexadecimalToBinary(hexNum);
printf("%lo", binaryToOctal(x));
break;
case 4:
printf("%d", hexadecimalToBinary(hexNum));
break;
default:
errorMessage();
}
}
void octalTo(int number, int to) {
int dec_value = 0, base = 1, temp = number;
long long x = octalToBinary(number);
switch (to) {
case 1:
while (temp) {
int last_digit = temp % 10;
temp /= 10;
dec_value += last_digit * base;
base *= 8;
}
printf("%d", dec_value);
break;
case 2:
binaryToHexadecimal(x);
break;
case 3:
printf("Gotchya, %d\n", number);
break;
case 4:
printf("%lld", octalToBinary(number));
break;
default:
errorMessage();
}
}
void binaryTo(long long number, int to) {
int num, decimal_val = 0, base = 1, rem;
switch (to) {
case 1:
while (number > 0) {
rem = number % 10;
decimal_val += rem * base;
number /= 10;
base *= 2;
}
printf("%d", decimal_val);
break;
case 2:
binaryToHexadecimal(number);
break;
case 3:
printf("%lo", binaryToOctal(number));
break;
case 4:
printf("Gotchya, %d\n", number);
break;
default:
errorMessage();
}
}
void operation() {
printf("\nSelect an option to convert your number \"to\" - \n");
selectOption();
printf(">>> ");
scanf("%d", &to);
}
int main() {
printf("------------------------------------------");
printf("\n### Number System Conversion by Soley! ###\n");
printf(">> To exit the program, use : CTRL + C <<\n");
printf("------------------------------------------\n\n");
do {
printf("Select an option to convert your number \"from\" - \n");
selectOption();
printf(">>> ");
scanf("%d", &from);
switch(from) {
case 1:
printf("\nEnter the number you want to convert: ");
scanf("%d", &number);
operation();
decimalTo(number, to);
break;
case 2:
printf("\nEnter the number you want to convert: ");
scanf("%s", &hexNum);
operation();
hexadecimalTo(hexNum, to);
break;
case 3:
printf("\nEnter the number you want to convert: ");
scanf("%d", &number);
operation();
octalTo(number, to);
break;
case 4:
printf("\nEnter the number you want to convert: ");
scanf("%lld", &binNumber);
operation();
binaryTo(binNumber, to);
break;
default:
errorMessage();
}
printf("\n\n");
} while(1);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment