Algorithms_in_C 1.0.0
Set of algorithms implemented in C.
Loading...
Searching...
No Matches
octal_to_hexadecimal.c File Reference

Octal to hexadecimal conversion by scanning user input. More...

#include <assert.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
Include dependency graph for octal_to_hexadecimal.c:

Functions

long octalToDecimal (long octalValue)
 Convert octal number to decimal number.
 
char * octalToHexadecimal (long octalValue)
 Convert octal number to hexadecimal number dynamically allocated memory needs to be freed by the calling the function free.
 
static void test ()
 Test function.
 
int main ()
 Main function.
 

Detailed Description

Octal to hexadecimal conversion by scanning user input.

The octalToHexadecimal function take the octal number as long return a string hexadecimal value after conversion

Author
Rachit Bhalla

Function Documentation

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
61{
62 // execute the tests
63 test();
64
65 // get the value of octal number as input
66 int octalValue;
67 printf("Enter an octal number: ");
68 scanf("%d", &octalValue);
69
70 // call the function octalToHexadecimal and print the hexadecimal value
71 char *hexadecimalValue = octalToHexadecimal(octalValue);
72 printf("Equivalent hexadecimal number is: %s", hexadecimalValue);
73
74 // free the memory allocated dynamically in function octalToHexadecimal
75 free(hexadecimalValue);
76
77 // return 0 and exit
78 return 0;
79}
#define free(ptr)
This macro replace the standard free function with free_dbg.
Definition malloc_dbg.h:26
char * octalToHexadecimal(long octalValue)
Convert octal number to hexadecimal number dynamically allocated memory needs to be freed by the call...
Definition octal_to_hexadecimal.c:38
static void test()
Test function.
Definition octal_to_hexadecimal.c:48
Here is the call graph for this function:

◆ octalToDecimal()

long octalToDecimal ( long  octalValue)

Convert octal number to decimal number.

Parameters
octalValueis the octal number that needs to be converted
Returns
a decimal number after conversion
20 {
21 long decimalValue = 0;
22 int i = 0;
23 while (octalValue) {
24 // Extracts right-most digit, multiplies it with 8^i, and increment i by 1
25 decimalValue += (long)(octalValue % 10) * pow(8, i++);
26 // Shift right in base 10
27 octalValue /= 10;
28 }
29 return decimalValue;
30}

◆ octalToHexadecimal()

char * octalToHexadecimal ( long  octalValue)

Convert octal number to hexadecimal number dynamically allocated memory needs to be freed by the calling the function free.

Parameters
octalValueis the octal number that needs to be converted
Returns
a hexadecimal value as a string after conversion
38 {
39 char *hexadecimalValue = malloc(256 * sizeof(char));
40 sprintf(hexadecimalValue, "%lX", octalToDecimal(octalValue));
41 return hexadecimalValue;
42}
#define malloc(bytes)
This macro replace the standard malloc function with malloc_dbg.
Definition malloc_dbg.h:18
long octalToDecimal(long octalValue)
Convert octal number to decimal number.
Definition octal_to_hexadecimal.c:20
Here is the call graph for this function:

◆ test()

static void test ( void  )
static

Test function.

Returns
void
48 {
49 /* test that hexadecimal value of octal number 213 is 8B */
50 assert(strcmp(octalToHexadecimal(213), "8B") == 0);
51
52 /* test that hexadecimal value of octal number 174 is 7C */
53 assert(strcmp(octalToHexadecimal(174), "7C") == 0);
54}
Here is the call graph for this function: