How to convert hexadecimal numbers to octal
Hexadecimal numbers are numbers with base 16. The numbers that lie in this category range from 0-15. 0-9 are represented by their digits, and 10-15 are represented by characters 10, and F representing 154 bits are required to represent a hexadecimal number.
Octal numbers are numbers with base 8. The numbers that lie in this category range from 0-7. Their respective digits represent them. A maximum of 3 bits are required to represent the octal numbers.
Converting hexadecimal to octal
In order to convert a hexadecimal to an octal, you need to follow a series of steps. Look at the slides below to understand more clearly:
1 of 5
Code
#include <iostream>#include <string>#include <cmath>using namespace std;int main(){int decimalNum=0, oct[10];string hexDecNum = "2AE";int val;int len = hexDecNum.length()-1;int i=0;while(len>=0){val = hexDecNum[len];if(val>=48 && val<=57)val = val-48;else if(val>=65 && val<=70)val = val-55;else if(val>=97 && val<=102)val = val-87;decimalNum += (val*pow(16, i));len--;i++;}i=0;while(decimalNum != 0){oct[i] = decimalNum%8;i++;decimalNum = decimalNum/8;}cout<<"Octal Value = ";for(i=(i-1); i>=0; i--)cout<<oct[i];cout<<endl;return 0;}
Free Resources
Copyright ©2025 Educative, Inc. All rights reserved