From 881e1e9a888687e1fd59c9b5f49103923f0631f3 Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Sun, 18 Oct 2020 18:00:23 +0530 Subject: [PATCH 1/7] 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; +} From 6b1c8e65be5117770211a26734c1f378b0a9b2c5 Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Sun, 18 Oct 2020 18:11:49 +0530 Subject: [PATCH 2/7] included stdio.h Fixes: #633 --- conversions/octal_to_hexadecimal.c | 1 + 1 file changed, 1 insertion(+) diff --git a/conversions/octal_to_hexadecimal.c b/conversions/octal_to_hexadecimal.c index d08acac0..86236e05 100644 --- a/conversions/octal_to_hexadecimal.c +++ b/conversions/octal_to_hexadecimal.c @@ -1,5 +1,6 @@ #include #include +#include // Converts octal number to decimal number long octalToDecimal(long octalValue){ From 5e11041d41eb236de1bc17bc7a9eaf7b8265b0dc Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Sun, 18 Oct 2020 23:48:36 +0530 Subject: [PATCH 3/7] added test cases and updated the documentation Fixes: #633 --- conversions/octal_to_hexadecimal.c | 42 ++++++++++++++++++++++++++++-- 1 file changed, 40 insertions(+), 2 deletions(-) diff --git a/conversions/octal_to_hexadecimal.c b/conversions/octal_to_hexadecimal.c index 86236e05..521064b2 100644 --- a/conversions/octal_to_hexadecimal.c +++ b/conversions/octal_to_hexadecimal.c @@ -1,8 +1,21 @@ +/** + * @brief Octal to hexadecimal conversion by scanning user input + * @details + * The octalToHexadecimal function take the octal number as long + * return a string hexadecimal value after conversion + * @author [Rachit Bhalla](https://github.com/rachitbhalla) + */ +#include #include #include #include +#include -// Converts octal number to decimal number +/** + * @brief Convert octal number to decimal number + * @param octalValue is the octal number that needs to be converted + * @returns A decimal number after conversion + */ long octalToDecimal(long octalValue){ long decimalValue = 0; int i = 0; @@ -14,15 +27,40 @@ long octalToDecimal(long octalValue){ } return decimalValue; } + +/** + * @brief Convert octal number to hexadecimal number + * @param octalValue is the octal number that needs to be converted + * @returns A hexadecimal value as a string after conversion + */ char *octalToHexadecimal(long octalValue){ char *hexadecimalValue = malloc(256 * sizeof(char)); sprintf(hexadecimalValue, "%lX", octalToDecimal(octalValue)); return hexadecimalValue; } + +/** + * @brief Test function + * @returns void + */ +static void test() { + /* test that hexadecimal value of octal number 213 is 8B */ + assert(strcmp(octalToHexadecimal(213), "8B") == 0); + + /* test that hexadecimal value of octal number 174 is 7C */ + assert(strcmp(octalToHexadecimal(174), "7C") == 0); +} + +/** + * @brief Main function + * @returns 0 on exit + */ int main() { - int octalValue; + //execute the tests + test(); + int octalValue; printf("Enter an octal number: "); scanf("%d", &octalValue); From 19fb5ba4dd7161357543b9ea63efa8d21889f953 Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Mon, 19 Oct 2020 00:53:48 +0530 Subject: [PATCH 4/7] updated with the suggested changes --- conversions/octal_to_hexadecimal.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/conversions/octal_to_hexadecimal.c b/conversions/octal_to_hexadecimal.c index 521064b2..e18edd20 100644 --- a/conversions/octal_to_hexadecimal.c +++ b/conversions/octal_to_hexadecimal.c @@ -14,7 +14,7 @@ /** * @brief Convert octal number to decimal number * @param octalValue is the octal number that needs to be converted - * @returns A decimal number after conversion + * @returns a decimal number after conversion */ long octalToDecimal(long octalValue){ long decimalValue = 0; @@ -31,7 +31,7 @@ long octalToDecimal(long octalValue){ /** * @brief Convert octal number to hexadecimal number * @param octalValue is the octal number that needs to be converted - * @returns A hexadecimal value as a string after conversion + * @returns a hexadecimal value as a string after conversion */ char *octalToHexadecimal(long octalValue){ char *hexadecimalValue = malloc(256 * sizeof(char)); From e1b775000a3e992c6d77a3417594d08b15aa87e4 Mon Sep 17 00:00:00 2001 From: github-actions <${GITHUB_ACTOR}@users.noreply.github.com> Date: Sun, 18 Oct 2020 19:25:13 +0000 Subject: [PATCH 5/7] updating DIRECTORY.md --- DIRECTORY.md | 1 + 1 file changed, 1 insertion(+) diff --git a/DIRECTORY.md b/DIRECTORY.md index e9c9426f..9f6a6e59 100644 --- a/DIRECTORY.md +++ b/DIRECTORY.md @@ -20,6 +20,7 @@ * [Int To String](https://github.com/TheAlgorithms/C/blob/master/conversions/int_to_string.c) * [Octal To Binary](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_binary.c) * [Octal To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_decimal.c) + * [Octal To Hexadecimal](https://github.com/TheAlgorithms/C/blob/master/conversions/octal_to_hexadecimal.c) * [To Decimal](https://github.com/TheAlgorithms/C/blob/master/conversions/to_decimal.c) ## Data Structures From e2444cee186e0b70d124944afd6c74779000d25c Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Mon, 19 Oct 2020 01:09:29 +0530 Subject: [PATCH 6/7] updated with the suggested changes --- conversions/octal_to_hexadecimal.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/conversions/octal_to_hexadecimal.c b/conversions/octal_to_hexadecimal.c index e18edd20..9ab706c2 100644 --- a/conversions/octal_to_hexadecimal.c +++ b/conversions/octal_to_hexadecimal.c @@ -5,11 +5,11 @@ * return a string hexadecimal value after conversion * @author [Rachit Bhalla](https://github.com/rachitbhalla) */ -#include -#include -#include -#include -#include +#include // for assert +#include // for pow function +#include // for scanf and printf functions +#include // for malloc and free functions +#include // for strcmp function /** * @brief Convert octal number to decimal number @@ -57,16 +57,16 @@ static void test() { */ int main() { - //execute the tests + // execute the tests test(); int octalValue; printf("Enter an octal number: "); scanf("%d", &octalValue); - //Calling the function octalToHexadecimal + // Calling the function octalToHexadecimal char *hexadecimalValue = octalToHexadecimal(octalValue); - printf("\nEquivalent hexadecimal number is: %s", hexadecimalValue); + printf("Equivalent hexadecimal number is: %s", hexadecimalValue); free(hexadecimalValue); return 0; } From f2c9fe3ee5529df44a9388f846fe4a0090c92c97 Mon Sep 17 00:00:00 2001 From: Rachit Bhalla Date: Tue, 20 Oct 2020 10:49:16 +0530 Subject: [PATCH 7/7] updated with the suggested changes --- conversions/octal_to_hexadecimal.c | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/conversions/octal_to_hexadecimal.c b/conversions/octal_to_hexadecimal.c index 9ab706c2..6c975810 100644 --- a/conversions/octal_to_hexadecimal.c +++ b/conversions/octal_to_hexadecimal.c @@ -1,4 +1,5 @@ /** + * @file * @brief Octal to hexadecimal conversion by scanning user input * @details * The octalToHexadecimal function take the octal number as long @@ -30,6 +31,7 @@ long octalToDecimal(long octalValue){ /** * @brief Convert octal number to hexadecimal number + * dynamically allocated memory needs to be freed by the calling the function free * @param octalValue is the octal number that needs to be converted * @returns a hexadecimal value as a string after conversion */ @@ -60,13 +62,18 @@ int main() // execute the tests test(); + // get the value of octal number as input int octalValue; printf("Enter an octal number: "); scanf("%d", &octalValue); - // Calling the function octalToHexadecimal + // call the function octalToHexadecimal and print the hexadecimal value char *hexadecimalValue = octalToHexadecimal(octalValue); printf("Equivalent hexadecimal number is: %s", hexadecimalValue); + + // free the memory allocated dynamically in function octalToHexadecimal free(hexadecimalValue); + + // return 0 and exit return 0; }