Algorithms_in_C  1.0.0
Set of algorithms implemented in C.
hexadecimal_to_octal2.c File Reference

Convert hexadecimal number to octal number (with decimal intermediary) More...

#include <stdio.h>
#include <string.h>
Include dependency graph for hexadecimal_to_octal2.c:

Macros

#define MAX_OCT_STR_LEN   23 /* 17_7777_7777_7777_7777_7777 */
 
#define MAX_HEX_STR_LEN   17 /* FFFF_FFFF_FFFF_FFFF */
 

Functions

const char * hex_to_oct (const char *hex)
 for printf() and fgets() More...
 
int main ()
 Main function. More...
 

Detailed Description

Convert hexadecimal number to octal number (with decimal intermediary)

The input is valid from 0 to 0xFFFF_FFFF_FFFF_FFFF.

At first, this program converts a hex string to an unsigned long long decimal, and then to an octal string.

When there is an invalid character in input string, this program stops parsing and converts the string until that character.

See also
hexadecimal_to_octal.c

Function Documentation

◆ hex_to_oct()

const char* hex_to_oct ( const char *  hex)

for printf() and fgets()

for memset()

Convert a hexadecimal number to octal number.

Parameters
hexHexadecimal number to convert.
Returns
A pointer to the converted octal string.
25 {
26 #define MAX_OCT_STR_LEN 23 /* 17_7777_7777_7777_7777_7777 */
27  static char octal[MAX_OCT_STR_LEN];
28  memset(octal, '\0', MAX_OCT_STR_LEN); // Initialize as NULL string
29 
30  unsigned long long decimal = 0;
31  int i = 0;
32  int len;
33 
34  if (hex == NULL)
35  {
36  // Return an empty string
37  return octal;
38  }
39 
40  /* Hexadecimal to decimal conversion */
41  while (*hex != '\n' && *hex != '\0')
42  {
43  char ch = *hex;
44 
45  if (ch >= '0' && ch <= '9')
46  {
47  ch -= '0';
48  }
49  else if (ch >= 'a' && ch <= 'f')
50  {
51  ch = ch - 'a' + 10;
52  }
53  else if (ch >= 'A' && ch <= 'F')
54  {
55  ch = ch - 'A' + 10;
56  }
57  else
58  {
59  printf("Invalid hexadecimal input: %c\n", ch);
60  break;
61  }
62 
63  decimal *= 16;
64  decimal += ch;
65  hex++;
66  }
67 
68  /* Decimal to octal conversion */
69  if (decimal == 0)
70  {
71  octal[0] = '0';
72  len = 1;
73  }
74  else
75  {
76  i = 0;
77  while (decimal > 0)
78  {
79  octal[i] = '0' + decimal % 8;
80  i++;
81  decimal /= 8;
82  }
83 
84  len = i;
85  }
86 
87  octal[len] = '\0';
88 
89  /* Reverse the octal string */
90  for (i = 0; i < len / 2; i++)
91  {
92  char tmp = octal[i];
93  octal[i] = octal[len - i - 1];
94  octal[len - i - 1] = tmp;
95  }
96 
97  return octal;
98 }

◆ main()

int main ( void  )

Main function.

Returns
0 on exit
105 {
106 #define MAX_HEX_STR_LEN 17 /* FFFF_FFFF_FFFF_FFFF */
107  char hex[MAX_HEX_STR_LEN];
108 
109  /* Input hexadecimal number from user */
110  printf("Enter any hexadecimal number: ");
111  fgets(hex, MAX_HEX_STR_LEN, stdin);
112 
113  const char *octal = hex_to_oct(hex);
114 
115  printf("Hexadecimal number = %s\n", hex);
116  printf("Octal number = %s\n", octal);
117 
118  return 0;
119 }
const char * hex_to_oct(const char *hex)
for printf() and fgets()
Definition: hexadecimal_to_octal2.c:24