Skip to content

Instantly share code, notes, and snippets.

@Ayushjhax
Last active May 2, 2024 09:10
Show Gist options
  • Save Ayushjhax/c7f1d7397ce9a83d2413ef8fcaf3e9f0 to your computer and use it in GitHub Desktop.
Save Ayushjhax/c7f1d7397ce9a83d2413ef8fcaf3e9f0 to your computer and use it in GitHub Desktop.
Experiment No.2 Implement a C program to convert a Hexadecimal, octal, and binary number vice versa.
// Name: Ayush Kumar Jha
// Enrollment Number: 2022-350-009
// Subject: Computer Organisation and Architecture + Operating System B.Tech CSE AI 406
// Experiment No. 2 Implement a C program to convert a Hexadecimal, octal, and binary number vice versa.
#include <stdio.h>
#include <string.h>
#include <math.h>
// Function to convert hexadecimal to decimal
int hexToDecimal(char hex[]) {
int len = strlen(hex);
int decimal = 0;
int base = 1;
for (int i = len - 1; i >= 0; i--) {
int digit;
if (hex[i] >= '0' && hex[i] <= '9') {
digit = hex[i] - '0';
} else if (hex[i] >= 'A' && hex[i] <= 'F') {
digit = hex[i] - 'A' + 10;
} else if (hex[i] >= 'a' && hex[i] <= 'f') {
digit = hex[i] - 'a' + 10;
} else {
printf("Invalid hexadecimal number\n");
return -1;
}
decimal += digit * base;
base *= 16;
}
return decimal;
}
// Function to convert decimal to hexadecimal
void decimalToHex(int decimal) {
char hex[20];
int i = 0;
while (decimal != 0) {
int remainder = decimal % 16;
if (remainder < 10)
hex[i++] = remainder + 48;
else
hex[i++] = remainder + 55;
decimal = decimal / 16;
}
printf("Hexadecimal: ");
for (int j = i - 1; j >= 0; j--) {
printf("%c", hex[j]);
}
printf("\n");
}
// Function to convert octal to decimal
int octalToDecimal(int octal) {
int decimal = 0, i = 0;
while (octal != 0) {
decimal += (octal % 10) * (1 << (3*i));
++i;
octal /= 10;
}
return decimal;
}
// Function to convert decimal to octal
int decimalToOctal(int decimal) {
int octal = 0, i = 1;
while (decimal != 0) {
octal += (decimal % 8) * i;
decimal /= 8;
i *= 10;
}
return octal;
}
// Function to convert binary to decimal
int binaryToDecimal(long long binary) {
int decimal = 0, i = 0;
while (binary != 0) {
decimal += (binary % 10) * pow(2, i);
++i;
binary /= 10;
}
return decimal;
}
// Function to convert decimal to binary
long long decimalToBinary(int decimal) {
long long binary = 0;
int remainder, i = 1;
while (decimal != 0) {
remainder = decimal % 2;
decimal /= 2;
binary += remainder * i;
i *= 10;
}
return binary;
}
int main() {
int choice, decimal, number;
long long binary;
char hex[20];
printf("Choose conversion:\n");
printf("1. Hexadecimal to Decimal\n");
printf("2. Decimal to Hexadecimal\n");
printf("3. Octal to Decimal\n");
printf("4. Decimal to Octal\n");
printf("5. Binary to Decimal\n");
printf("6. Decimal to Binary\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch(choice) {
case 1:
printf("Enter a hexadecimal number: ");
scanf("%s", hex);
decimal = hexToDecimal(hex);
if (decimal != -1) {
printf("Decimal: %d\n", decimal);
}
break;
case 2:
printf("Enter a decimal number: ");
scanf("%d", &decimal);
decimalToHex(decimal);
break;
case 3:
printf("Enter an octal number: ");
scanf("%o", &number);
printf("Decimal: %d\n", octalToDecimal(number));
break;
case 4:
printf("Enter a decimal number: ");
scanf("%d", &number);
printf("Octal: %o\n", decimalToOctal(number));
break;
case 5:
printf("Enter a binary number: ");
scanf("%lld", &binary);
printf("Decimal: %d\n", binaryToDecimal(binary));
break;
case 6:
printf("Enter a decimal number: ");
scanf("%d", &decimal);
printf("Binary: %lld\n", decimalToBinary(decimal));
break;
default:
printf("Invalid choice!\n");
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment