Javatpoint Logo
Javatpoint Logo

Octal to Hexadecimal in C

What is an octal number system?

An octal number system is a number system which is having base-8 where base-8 represents the eight digits from 0 to 7.

What is a Hexadecimal number system?

A hexadecimal number system is a number system which is having base-16 where base-16 represents the sixteen digits, i.e.,0,1,2,3,4,5,6,7,8,9, A, B, C, D, E, F.

How to convert an octal number into a hexadecimal number

We cannot directly convert an octal number system into a hexadecimal number system. First, we need to convert the octal number into a binary number system, and then this binary number system will be converted into a hexadecimal number system.

Let's look at the approach.

  • Input the octal number.
  • An octal number system is converted into a binary number system.
  • Extract the 4 bits in a group from the right-side.
  • Provide the hexadecimal number to the extracted 4 bits.

For example, the octal number is 123.

Octal to Hexadecimal in C

Let's create a simple program which converts the octal into a hexadecimal number.

Output

Octal to Hexadecimal in C

Analysis of the above program

  • First, we take the user input, which would be an octal number, and it gets stored in an 'octal' variable.
  • After entering the user input, we convert the octal number into binary number. We iterate through the while(octal!=0) loop, which executes till the value of octal is not equal to zero. The final value of a binary number will be stored in a 'binary' variable.
  • After calculating the binary number, we will calculate the hexadecimal number. We will iterate through the elements of a binary number by using a while(binary>0) On each iteration, four digits from the right-side of a binary number are extracted and replaced with their corresponding hexadecimal number.
  • The hexadecimal[] variable contains the reverse value of the actual hexadecimal number. In order to get the actual hexadecimal number of an octal number, we define a new variable, i.e., hex[]. We will iterate the elements of a hexadecimal[] variable from the last, and stores each element in hex[].

Next TopicStrong number in C



Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA