From 881e1e9a888687e1fd59c9b5f49103923f0631f3 Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Sun, 18 Oct 2020 18:00:23 +0530 Subject: [PATCH] Implemented octal to hexadecimal conversion Fixes: #633 --- conversions/octal_to_hexadecimal.c | 33 ++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+) create mode 100644 conversions/octal_to_hexadecimal.c diff --git a/conversions/octal_to_hexadecimal.c b/conversions/octal_to_hexadecimal.c new file mode 100644 index 00000000..d08acac0 --- /dev/null +++ b/conversions/octal_to_hexadecimal.c @@ -0,0 +1,33 @@ +#include +#include + +// Converts octal number to decimal number +long octalToDecimal(long octalValue){ + long decimalValue = 0; + int i = 0; + while (octalValue) { + // Extracts right-most digit, multiplies it with 8^i, and increment i by 1 + decimalValue += (long)(octalValue % 10) * pow(8, i++); + // Shift right in base 10 + octalValue /= 10; + } + return decimalValue; +} +char *octalToHexadecimal(long octalValue){ + char *hexadecimalValue = malloc(256 * sizeof(char)); + sprintf(hexadecimalValue, "%lX", octalToDecimal(octalValue)); + return hexadecimalValue; +} +int main() +{ + int octalValue; + + printf("Enter an octal number: "); + scanf("%d", &octalValue); + + //Calling the function octalToHexadecimal + char *hexadecimalValue = octalToHexadecimal(octalValue); + printf("\nEquivalent hexadecimal number is: %s", hexadecimalValue); + free(hexadecimalValue); + return 0; +}