Given a Hexadecimal number, the task is to convert it into an Octal number.
Examples:
Input: Hexadecimal = 1AC Output: Binary = 0654 Explanation: Equivalent binary value of 1: 0001 Equivalent binary value of A: 1010 Equivalent binary value of C: 1100 Grouping in terms of 3: 000 110 101 100 Equivalent octal value of 000: 0 Equivalent octal value of 110: 6 Equivalent octal value of 101: 5 Equivalent octal value of 100: 4 Input: Hexadecimal = 5D1F Output: Octal = 56437
Approach:
A hexadecimal number is a positional numeral system with a radix, or base, of 16 and uses sixteen distinct symbols.
An octal number or oct for short is the base-8 number and uses the digits 0 to 7. Octal numerals can be made from binary numerals by grouping consecutive binary digits into groups of three (starting from the right) when it is before the radix point and if the binary digits are after the radix point then start from the left.
Steps of Conversion
- Find the equivalent binary number for each digit of the given hexadecimal number. Add 0’s to the left if any of the binary equivalents is shorter than 4 bits.
- Separate the binary digits into groups, each containing 3 bits or digits from right to left. Add 0s to the left, if the last group contains less than 3 bits.
- Find the octal equivalent for each binary group.
Below is the implementation of the above approach:
// C++ program to convert // Hexadecimal to Octal #include <bits/stdc++.h> using namespace std;
// Function to convert HexaDecimal to Binary long long int hex_to_bin( char hex[])
{ long long int bin, place;
int i = 0, rem, val;
bin = 0ll;
place = 0ll;
// Hexadecimal to binary conversion
for (i = 0; hex[i] != '\0' ; i++) {
bin = bin * place;
switch (hex[i]) {
case '0' :
bin += 0;
break ;
case '1' :
bin += 1;
break ;
case '2' :
bin += 10;
break ;
case '3' :
bin += 11;
break ;
case '4' :
bin += 100;
break ;
case '5' :
bin += 101;
break ;
case '6' :
bin += 110;
break ;
case '7' :
bin += 111;
break ;
case '8' :
bin += 1000;
break ;
case '9' :
bin += 1001;
break ;
case 'a' :
case 'A' :
bin += 1010;
break ;
case 'b' :
case 'B' :
bin += 1011;
break ;
case 'c' :
case 'C' :
bin += 1100;
break ;
case 'd' :
case 'D' :
bin += 1101;
break ;
case 'e' :
case 'E' :
bin += 1110;
break ;
case 'f' :
case 'F' :
bin += 1111;
break ;
default :
cout << "Invalid hexadecimal input." ;
}
place = 10000;
}
return bin;
} // Function to convert Binary to Octal long long int bin_to_oct( long long bin)
{ long long int octal, place;
int i = 0, rem, val;
octal = 0ll;
place = 0ll;
place = 1;
// Binary to octal conversion
while (bin > 0) {
rem = bin % 1000;
switch (rem) {
case 0:
val = 0;
break ;
case 1:
val = 1;
break ;
case 10:
val = 2;
break ;
case 11:
val = 3;
break ;
case 100:
val = 4;
break ;
case 101:
val = 5;
break ;
case 110:
val = 6;
break ;
case 111:
val = 7;
break ;
}
octal = (val * place) + octal;
bin /= 1000;
place *= 10;
}
return octal;
} // Function to Convert // Hexadecimal Number to Octal Number long long int hex_to_oct( char hex[])
{ long long int octal, bin;
// convert HexaDecimal to Binary
bin = hex_to_bin(hex);
// convert Binary to Octal
octal = bin_to_oct(bin);
return octal;
} // driver code int main()
{ // Get the hexadecimal number char hex[20] = "1AC" ;
// convert hexadecimal to octal cout << "Equivalent Octal Value = " << hex_to_oct(hex);
return 0;
} //This code is contributed by shubhamsingh10 |
// C program to convert // Hexadecimal to Octal #include <math.h> #include <stdio.h> #include <string.h> // Function to convert HexaDecimal to Binary long long int hex_to_bin( char hex[])
{ long long int bin, place;
int i = 0, rem, val;
bin = 0ll;
place = 0ll;
// Hexadecimal to binary conversion
for (i = 0; hex[i] != '\0' ; i++) {
bin = bin * place;
switch (hex[i]) {
case '0' :
bin += 0;
break ;
case '1' :
bin += 1;
break ;
case '2' :
bin += 10;
break ;
case '3' :
bin += 11;
break ;
case '4' :
bin += 100;
break ;
case '5' :
bin += 101;
break ;
case '6' :
bin += 110;
break ;
case '7' :
bin += 111;
break ;
case '8' :
bin += 1000;
break ;
case '9' :
bin += 1001;
break ;
case 'a' :
case 'A' :
bin += 1010;
break ;
case 'b' :
case 'B' :
bin += 1011;
break ;
case 'c' :
case 'C' :
bin += 1100;
break ;
case 'd' :
case 'D' :
bin += 1101;
break ;
case 'e' :
case 'E' :
bin += 1110;
break ;
case 'f' :
case 'F' :
bin += 1111;
break ;
default :
printf ( "Invalid hexadecimal input." );
}
place = 10000;
}
return bin;
} // Function to convert Binary to Octal long long int bin_to_oct( long long bin)
{ long long int octal, place;
int i = 0, rem, val;
octal = 0ll;
place = 0ll;
place = 1;
// Binary to octal conversion
while (bin > 0) {
rem = bin % 1000;
switch (rem) {
case 0:
val = 0;
break ;
case 1:
val = 1;
break ;
case 10:
val = 2;
break ;
case 11:
val = 3;
break ;
case 100:
val = 4;
break ;
case 101:
val = 5;
break ;
case 110:
val = 6;
break ;
case 111:
val = 7;
break ;
}
octal = (val * place) + octal;
bin /= 1000;
place *= 10;
}
return octal;
} // Function to Convert // Hexadecimal Number to Octal Number long long int hex_to_oct( char hex[])
{ long long int octal, bin;
// convert HexaDecimal to Binary
bin = hex_to_bin(hex);
// convert Binary to Octal
octal = bin_to_oct(bin);
return octal;
} // driver code int main()
{ // Get the hexadecimal number char hex[20] = "1AC" ;
// convert hexadecimal to octal printf ( "Equivalent Octal Value = %lld" ,
hex_to_oct(hex));
return 0;
} |
// Java program to convert // Hexadecimal to Octal class hex_to_oct
{ // declaring main method. public static void main(String[] args)
{ int dec = 0 ;
// taking 1AC as an example of hexadecimal Number.
String hexa = "1AC" ;
int c = hexa.length() - 1 ;
// finding the decimal equivalent of the
// hexa decimal number
for ( int i = 0 ; i < hexa.length() ; i ++ )
{
//extracting each character from the string.
char ch = hexa.charAt(i);
switch (ch)
{
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
dec = dec + Integer.parseInt(Character.toString(ch))*
( int )Math.pow( 16 ,c);
c--;
break ;
case 'a' :
case 'A' :
dec = dec + 10 * ( int )Math.pow( 16 , c);
c--;
break ;
case 'b' :
case 'B' :
dec = dec + 11 * ( int )Math.pow( 16 , c);
c--;
break ;
case 'c' :
case 'C' :
dec = dec + 12 * ( int )Math.pow( 16 , c);
c--;
break ;
case 'd' :
case 'D' :
dec = dec + 13 * ( int )Math.pow( 16 , c);
c--;
break ;
case 'e' :
case 'E' :
dec = dec + 14 * ( int )Math.pow( 16 , c);
c--;
break ;
case 'f' :
case 'F' :
dec = dec + 15 * ( int )Math.pow( 16 , c);
c--;
break ;
default :
System.out.println( "Invalid hexa input" );
break ;
}
}
// String oct to store the octal equivalent of a hexadecimal number.
String oct = "" ;
//converting decimal to octal number.
while (dec > 0 )
{
oct = dec % 8 + oct;
dec = dec / 8 ;
}
// Printing the final output.
System.out.println( "Equivalent Octal Value = " +oct);
} } // This code is contributed by Animesh_Gupta |
# importing math package import math
hex = "1AC"
# variable oct to store # octal equivalent of hexa decimal # number returned from the method oct = ""
dec = i = 0
c = len ( hex ) - 1
# loop to extract each digit of number while i < len ( hex ):
# digit extracted
d = hex [i]
if d = = '0' or d = = '1' or d = = '2' or \
d = = '3' or d = = '4' or d = = '5' :
dec = dec + int (d) * int (math. pow ( 16 , c))
elif d = = '6' or d = = '7' or d = = '8' or d = = '9' :
dec = dec + int (d) * int (math. pow ( 16 , c))
elif (d = = 'A' ) or (d = = 'a' ):
dec = dec + 10 * int (math. pow ( 16 , c))
elif (d = = 'B' ) or (d = = 'b' ):
dec = dec + 11 * int (math. pow ( 16 , c))
elif (d = = 'C' ) or (d = = 'c' ):
dec = dec + 12 * int (math. pow ( 16 , c))
elif (d = = 'D' ) or (d = = 'd' ):
dec = dec + 13 * int (math. pow ( 16 , c))
elif (d = = 'E' ) or (d = = 'e' ):
dec = dec + 14 * int (math. pow ( 16 , c))
elif (d = = 'F' ) or (d = = 'f' ):
dec = dec + 15 * int (math. pow ( 16 , c))
else :
print ( "invalid input" )
break
i + = 1
c - = 1
# loop to find octal equivalent # stored in dec i.e. # conversion of decimal to octal. while (dec > 0 ):
oct = "".join([ str ( int (dec % 8 )) , oct ])
dec = int (dec / 8 )
# printing the final result print ( "Equivalent Octal Value =" , oct )
# This code is contributed by 29AjayKumar |
// C# program to convert // Hexadecimal to Octal using System;
class GFG{
// Driver code public static void Main( string [] args)
{ int dec = 0;
// taking 1AC as an example
// of hexadecimal Number.
string hexa = "1AC" ;
int c = hexa.Length - 1;
// Finding the decimal
// equivalent of the
// hexa decimal number
for ( int i = 0; i < hexa.Length ; i ++ )
{
// Extracting each character
// from the string.
char ch = hexa[i];
switch (ch)
{
case '0' :
case '1' :
case '2' :
case '3' :
case '4' :
case '5' :
case '6' :
case '7' :
case '8' :
case '9' :
dec = dec + Int32.Parse(ch.ToString())*
( int )Math.Pow(16, c);
c--;
break ;
case 'a' :
case 'A' :
dec = dec + 10 * ( int )Math.Pow(16, c);
c--;
break ;
case 'b' :
case 'B' :
dec = dec + 11 * ( int )Math.Pow(16, c);
c--;
break ;
case 'c' :
case 'C' :
dec = dec + 12 * ( int )Math.Pow(16, c);
c--;
break ;
case 'd' :
case 'D' :
dec = dec + 13 * ( int )Math.Pow(16, c);
c--;
break ;
case 'e' :
case 'E' :
dec = dec + 14 * ( int )Math.Pow(16, c);
c--;
break ;
case 'f' :
case 'F' :
dec = dec + 15 * ( int )Math.Pow(16, c);
c--;
break ;
default :
Console.Write( "Invalid hexa input" );
break ;
}
}
// String oct to store the octal
// equivalent of a hexadecimal number.
string oct = "" ;
// converting decimal
// to octal number.
while (dec > 0)
{
oct = dec % 8 + oct;
dec = dec / 8;
}
// Printing the final output.
Console.Write( "Equivalent Octal Value = " +
oct);
} } // This code is contributed by rutvik_56 |
// JavaScript program to convert // Hexadecimal to Octal // Function to convert HexaDecimal to Binary function hex_to_bin(hex)
{ var bin, place;
var i = 0, rem, val;
bin = 0;
place = 0;
// Hexadecimal to binary conversion
for (i = 0; i < hex.length; i++) {
bin = bin * place;
switch (hex[i]) {
case '0' :
bin += 0;
break ;
case '1' :
bin += 1;
break ;
case '2' :
bin += 10;
break ;
case '3' :
bin += 11;
break ;
case '4' :
bin += 100;
break ;
case '5' :
bin += 101;
break ;
case '6' :
bin += 110;
break ;
case '7' :
bin += 111;
break ;
case '8' :
bin += 1000;
break ;
case '9' :
bin += 1001;
break ;
case 'a' :
case 'A' :
bin += 1010;
break ;
case 'b' :
case 'B' :
bin += 1011;
break ;
case 'c' :
case 'C' :
bin += 1100;
break ;
case 'd' :
case 'D' :
bin += 1101;
break ;
case 'e' :
case 'E' :
bin += 1110;
break ;
case 'f' :
case 'F' :
bin += 1111;
break ;
default :
console.log( "Invalid hexadecimal input." );
}
place = 10000;
}
return bin;
} // Function to convert Binary to Octal function bin_to_oct(bin)
{ var octal, place;
var i = 0, rem, val;
octal = 0;
place = 0;
place = 1;
// Binary to octal conversion
while (bin > 0) {
rem = bin % 1000;
switch (rem) {
case 0:
val = 0;
break ;
case 1:
val = 1;
break ;
case 10:
val = 2;
break ;
case 11:
val = 3;
break ;
case 100:
val = 4;
break ;
case 101:
val = 5;
break ;
case 110:
val = 6;
break ;
case 111:
val = 7;
break ;
}
octal = (val * place) + octal;
bin = Math.floor(bin/1000);
place *= 10;
}
return octal;
} // Function to Convert // Hexadecimal Number to Octal Number function hex_to_oct(hex)
{ var octal, bin;
// convert HexaDecimal to Binary
bin = hex_to_bin(hex);
// convert Binary to Octal
octal = bin_to_oct(bin);
return octal;
} // driver code // Get the hexadecimal number var hex = "1AC" ;
// convert hexadecimal to octal console.log( "Equivalent Octal Value = " + hex_to_oct(hex));
// This code is contributed by phasing17 |
Equivalent Octal Value = 654
Time complexity: O(N), where N is the length of a given hexadecimal number string
Auxiliary space: O(1)
Method 2: Using Decimal Conversion
1. A dictionary hex_to_dec_dict is defined which contains the decimal values of each hexadecimal digit.
2. The input hexadecimal number is iterated through one digit at a time.
3. The decimal value of each digit is calculated by multiplying the previous value by 16 and adding the current digit’s decimal value from the hex_to_dec_dict.
4. The resulting decimal number is then converted to an octal number.
5. A string octal_num is initialized to hold the octal number.
6. The decimal number is divided by 8 and the remainder is added as a digit to the left of the existing octal_num string.
7. The decimal number is then updated to the result of integer division by 8 (i.e. with the rightmost digit removed).
8. Steps 6-7 are repeated until the decimal number becomes zero.
9. The resulting octal_num string is returned as the output.
#include <iostream> #include <string> #include <unordered_map> using namespace std;
string hex_to_oct(string hex_num) { // Map containing hexadecimal to decimal conversion
unordered_map< char , int > hex_to_dec_map = {
{ '0' , 0}, { '1' , 1}, { '2' , 2}, { '3' , 3},
{ '4' , 4}, { '5' , 5}, { '6' , 6}, { '7' , 7},
{ '8' , 8}, { '9' , 9}, { 'A' , 10}, { 'B' , 11},
{ 'C' , 12}, { 'D' , 13}, { 'E' , 14}, { 'F' , 15}
};
// Converting hexadecimal number to decimal
int decimal_num = 0;
for ( char digit : hex_num) {
decimal_num = decimal_num * 16 + hex_to_dec_map[digit];
}
// Converting decimal number to octal
string octal_num = "" ;
while (decimal_num > 0) {
int octal_digit = decimal_num % 8;
octal_num = to_string(octal_digit) + octal_num;
decimal_num /= 8;
}
return octal_num;
} int main() {
string hex_num = "1AC" ;
cout << hex_to_oct(hex_num) << endl;
return 0;
} |
public class HexToOct {
public static String hexToOct(String hexNum) {
// Dictionary containing hexadecimal to decimal conversion
java.util.HashMap<Character, Integer> hexToDecMap = new java.util.HashMap<>();
hexToDecMap.put( '0' , 0 );
hexToDecMap.put( '1' , 1 );
hexToDecMap.put( '2' , 2 );
hexToDecMap.put( '3' , 3 );
hexToDecMap.put( '4' , 4 );
hexToDecMap.put( '5' , 5 );
hexToDecMap.put( '6' , 6 );
hexToDecMap.put( '7' , 7 );
hexToDecMap.put( '8' , 8 );
hexToDecMap.put( '9' , 9 );
hexToDecMap.put( 'A' , 10 );
hexToDecMap.put( 'B' , 11 );
hexToDecMap.put( 'C' , 12 );
hexToDecMap.put( 'D' , 13 );
hexToDecMap.put( 'E' , 14 );
hexToDecMap.put( 'F' , 15 );
// Converting hexadecimal number to decimal
int decimalNum = 0 ;
for ( int i = 0 ; i < hexNum.length(); i++) {
char digit = hexNum.charAt(i);
decimalNum = decimalNum * 16 + hexToDecMap.get(digit);
}
// Converting decimal number to octal
StringBuilder octalNumBuilder = new StringBuilder();
while (decimalNum > 0 ) {
int octalDigit = decimalNum % 8 ;
octalNumBuilder.insert( 0 , octalDigit);
decimalNum /= 8 ;
}
String octalNum = octalNumBuilder.toString();
return octalNum;
}
public static void main(String[] args) {
String hexNum = "1AC" ;
System.out.println(hexToOct(hexNum));
}
} |
def hex_to_oct(hex_num):
# Dictionary containing hexadecimal to decimal conversion
hex_to_dec_dict = {
'0' : 0 , '1' : 1 , '2' : 2 , '3' : 3 ,
'4' : 4 , '5' : 5 , '6' : 6 , '7' : 7 ,
'8' : 8 , '9' : 9 , 'A' : 10 , 'B' : 11 ,
'C' : 12 , 'D' : 13 , 'E' : 14 , 'F' : 15
}
# Converting hexadecimal number to decimal
decimal_num = 0
for digit in hex_num:
decimal_num = decimal_num * 16 + hex_to_dec_dict[digit]
# Converting decimal number to octal
octal_num = ''
while decimal_num > 0 :
octal_digit = decimal_num % 8
octal_num = str (octal_digit) + octal_num
decimal_num / / = 8
return octal_num
# Example Usage hex_num = "1AC"
print (hex_to_oct(hex_num))
|
// C# Program for the above approach using System;
using System.Collections.Generic;
namespace HexToOctConverter
{ class Program
{
static string HexToOct( string hexNum)
{
// Map containing hexadecimal to decimal conversion
Dictionary< char , int > hexToDecMap = new Dictionary< char , int >(){
{ '0' , 0}, { '1' , 1}, { '2' , 2}, { '3' , 3},
{ '4' , 4}, { '5' , 5}, { '6' , 6}, { '7' , 7},
{ '8' , 8}, { '9' , 9}, { 'A' , 10}, { 'B' , 11},
{ 'C' , 12}, { 'D' , 13}, { 'E' , 14}, { 'F' , 15}
};
// Converting hexadecimal number to decimal
int decimalNum = 0;
foreach ( char digit in hexNum)
{
decimalNum = decimalNum * 16 + hexToDecMap[digit];
}
// Converting decimal number to octal
string octalNum = "" ;
while (decimalNum > 0)
{
int octalDigit = decimalNum % 8;
octalNum = octalDigit.ToString() + octalNum;
decimalNum /= 8;
}
return octalNum;
}
static void Main( string [] args)
{
string hexNum = "1AC" ;
Console.WriteLine(HexToOct(hexNum));
Console.ReadLine();
}
}
} // This code is contributed by princekumaras |
// Javascript program for the above approach function hex_to_oct(hex_num) {
// Dictionary containing hexadecimal to decimal conversion
const hex_to_dec_dict = {
'0' : 0, '1' : 1, '2' : 2, '3' : 3,
'4' : 4, '5' : 5, '6' : 6, '7' : 7,
'8' : 8, '9' : 9, 'A' : 10, 'B' : 11,
'C' : 12, 'D' : 13, 'E' : 14, 'F' : 15
};
// Converting hexadecimal number to decimal
let decimal_num = 0;
for (let i = 0; i < hex_num.length; i++) {
decimal_num = decimal_num * 16 + hex_to_dec_dict[hex_num[i]];
}
// Converting decimal number to octal
let octal_num = '' ;
while (decimal_num > 0) {
let octal_digit = decimal_num % 8;
octal_num = octal_digit.toString() + octal_num;
decimal_num = Math.floor(decimal_num / 8);
}
return octal_num;
} // Example Usage let hex_num = "1AC" ;
console.log(hex_to_oct(hex_num)); // This code is contributed by rishabmalhdijo |
654
Time Complexity: O(n^2) where n is the number of digits in the hexadecimal number.
Auxiliary Space: O(1)