Posted on by Kalkicode
Code Conversion

# Convert octal to hexadecimal number

Octal and hexadecimal are two different numbering systems used to represent numbers in computing. Octal is a base-8 numbering system, which means it uses eight digits, namely 0, 1, 2, 3, 4, 5, 6, and 7, to represent numbers. Hexadecimal, on the other hand, is a base-16 numbering system that uses sixteen digits, namely 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A, B, C, D, E, and F, to represent numbers.

Converting an octal number to a hexadecimal number involves converting the octal number into its equivalent binary number and then converting the binary number to a hexadecimal number. The steps to convert an octal number to a hexadecimal number are as follows:

1. Write down the octal number.
2. Convert each octal digit to its equivalent binary digit (using 3 bits for each octal digit).
3. Group the binary digits into groups of four starting from the right.
4. Convert each group of four binary digits to its equivalent hexadecimal digit.
5. Write down the hexadecimal digits to get the final hexadecimal number.

Here's an example:

Convert the octal number 752 to a hexadecimal number.

1. The octal number is 752.
2. Converting each octal digit to its equivalent binary digit, we get: 7 -> 111 5 -> 101 2 -> 010
3. Grouping the binary digits into groups of four starting from the right, we get: 111 101 010 as [111101010]
4. Converting each group of four [0001,1110,1010] binary digits to its equivalent hexadecimal digit, we get: 1 E A
5. Write down the hexadecimal digits to get the final hexadecimal number: 1EA.

Therefore, the hexadecimal equivalent of the octal number 752 is 1EA.

## Program Solution

``````//C Program
#include <stdio.h>

void reverse(char str[], int start, int end)
{
char temp;
for (start; start < end; start++, end--)
{
temp = str[start];
str[start] = str[end];
str[end] = temp;
}
}
{
int digit = 0;
int index = 0;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result[index] = digit + 48;
}
else if (digit >= 10)
{
//55 ASCII of '7'
result[index] = digit + 55;
}
index++;
number /= 16;
}
if (index >= 1)
{
result[index] = '\0';
reverse(result, 0, index - 1);
}
}
long long octal_to_decimal(long long number)
{
long long result = 0, multiplier = 1;
int remainder = 0;
while (number != 0)
{
remainder = number % 10;
result = (remainder * multiplier) + result;
multiplier *= 8;
number /= 10;
}
return result;
}
void octal_to_hexa(long long number)
{
printf("Octal : %lld  ", number);
int flag = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = octal_to_decimal(number);
//Used to store result
char result[32];
if (flag == 1)
{
}
else
{
}
}
int main()
{
//Test Cases
octal_to_hexa(16);
octal_to_hexa(45);
octal_to_hexa(-35);
octal_to_hexa(63);
return 0;
}``````

#### Output

``````Octal : 16  Hexadecimal : E
Octal : 45  Hexadecimal : 25
Octal : -35  Hexadecimal : -1D
Octal : 63  Hexadecimal : 33``````
``````/*
Java Program
*/
class MyNumber
{
{
long digit = 0;
String result = "";
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = (char)(digit + 48) + result;
}
else if (digit >= 10)
{
//55 ASCII of '7'
result = (char)(digit + 55) + result;
}
number /= 16;
}
return result;
}
public long octal_to_decimal(long number)
{
long result = 0, multiplier = 1;
long remainder = 0;
while (number != 0)
{
remainder = number % 10;
result = (remainder * multiplier) + result;
multiplier *= 8;
number /= 10;
}
return result;
}
public void octal_to_hexa(long number)
{
System.out.print("Octal :  " + number);
int flag = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = octal_to_decimal(number);
if (flag == 1)
{
result = "-" + result;
}
System.out.print(" Hexadecimal : " + result + "\n\n");
}
public static void main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.octal_to_hexa(16);
obj.octal_to_hexa(45);
obj.octal_to_hexa(-35);
obj.octal_to_hexa(63);
}
}``````

#### Output

``````Octal :  16 Hexadecimal : E

Octal :  45 Hexadecimal : 25

Octal :  -35 Hexadecimal : -1D

Octal :  63 Hexadecimal : 33
``````
``````//Include header file
#include <iostream>

using namespace std;
/*
C++ Program
*/
class MyNumber
{
public:
{
long digit = 0;
string result = "";
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = (char)(digit + 48) + result;
}
else if (digit >= 10)
{
//55 ASCII of '7'
result = (char)(digit + 55) + result;
}
number /= 16;
}
return result;
}
long octal_to_decimal(long number)
{
long result = 0, multiplier = 1;
long remainder = 0;
while (number != 0)
{
remainder = number % 10;
result = (remainder * multiplier) + result;
multiplier *= 8;
number /= 10;
}
return result;
}
void octal_to_hexa(long number)
{
cout << "Octal :  " << number;
int flag = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = this->octal_to_decimal(number);
if (flag == 1)
{
result = "-" + result;
}
cout << " Hexadecimal : " << result << "\n\n";
}
};
int main()
{
MyNumber obj = MyNumber();
//Test Cases
obj.octal_to_hexa(16);
obj.octal_to_hexa(45);
obj.octal_to_hexa(-35);
obj.octal_to_hexa(63);
return 0;
}``````

#### Output

``````Octal :  16 Hexadecimal : E

Octal :  45 Hexadecimal : 25

Octal :  -35 Hexadecimal : -1D

Octal :  63 Hexadecimal : 33
``````
``````//Include namespace system
using System;
/*
C# Program
*/
class MyNumber
{
{
long digit = 0;
String result = "";
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = (char)(digit + 48) + result;
}
else if (digit >= 10)
{
//55 ASCII of '7'
result = (char)(digit + 55) + result;
}
number /= 16;
}
return result;
}
public long octal_to_decimal(long number)
{
long result = 0, multiplier = 1;
long remainder = 0;
while (number != 0)
{
remainder = number % 10;
result = (remainder * multiplier) + result;
multiplier *= 8;
number /= 10;
}
return result;
}
public void octal_to_hexa(long number)
{
Console.Write("Octal :  " + number);
int flag = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = octal_to_decimal(number);
if (flag == 1)
{
result = "-" + result;
}
Console.Write(" Hexadecimal : " + result + "\n\n");
}
public static void Main(String[] args)
{
MyNumber obj = new MyNumber();
//Test Cases
obj.octal_to_hexa(16);
obj.octal_to_hexa(45);
obj.octal_to_hexa(-35);
obj.octal_to_hexa(63);
}
}``````

#### Output

``````Octal :  16 Hexadecimal : E

Octal :  45 Hexadecimal : 25

Octal :  -35 Hexadecimal : -1D

Octal :  63 Hexadecimal : 33
``````
``````<?php
/*
Php Program
*/
class MyNumber
{
{
\$digit = 0;
\$result = "";
while (\$number != 0)
{
\$digit = \$number % 16;
if (\$digit < 0)
{
\$digit = -\$digit;
}
if (\$digit < 10)
{
//48 ASCII of '0'
\$result = chr((\$digit + 48)) . \$result;
}
else if (\$digit >= 10)
{
//55 ASCII of '7'
\$result = chr((\$digit + 55)) . \$result;
}
\$number = intval(\$number / 16);
}
return \$result;
}
public  function octal_to_decimal(\$number)
{
\$result = 0;
\$multiplier = 1;
\$remainder = 0;
while (\$number != 0)
{
\$remainder = \$number % 10;

\$result = (\$remainder *\$multiplier) + \$result;
\$multiplier *= 8;

\$number = intval(\$number / 10);
}
return \$result;
}
public  function octal_to_hexa(\$number)
{
echo("Octal : ". \$number);
\$flag = 0;
if (\$number < 0)
{
//When number is negative number
\$flag = 1;
\$number = -\$number;
}
\$number = \$this->octal_to_decimal(\$number);
if (\$flag == 1)
{
\$result = "-". \$result;
}
echo(" Hexadecimal : ". \$result ."\n\n");
}
}

function main()
{
\$obj = new MyNumber();
//Test Cases
\$obj->octal_to_hexa(16);
\$obj->octal_to_hexa(45);
\$obj->octal_to_hexa(-35);
\$obj->octal_to_hexa(63);
}
main();``````

#### Output

``````Octal : 16 Hexadecimal : E

Octal : 45 Hexadecimal : 25

Octal : -35 Hexadecimal : -1D

Octal : 63 Hexadecimal : 33
``````
``````/*
Node Js Program
*/
class MyNumber
{
{
var digit = 0;
var result = "";
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = String.fromCharCode((digit + 48)) + result;
}
else
if (digit >= 10)
{
//55 ASCII of '7'
result = String.fromCharCode((digit + 55)) + result;
}
number = parseInt(number / 16);
}
return result;
}
octal_to_decimal(number)
{
var result = 0;
var multiplier = 1;
var remainder = 0;
while (number != 0)
{
remainder = number % 10;

result = (remainder *multiplier) + result;
multiplier *= 8;

number = parseInt(number / 10);
}
return result;
}
octal_to_hexa(number)
{
process.stdout.write("Octal : " + number);
var flag = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = this.octal_to_decimal(number);
if (flag == 1)
{
result = "-" + result;
}
process.stdout.write(" Hexadecimal : " + result + "\n\n");
}
}

function main(args)
{
var obj = new MyNumber();
//Test Cases
obj.octal_to_hexa(16);
obj.octal_to_hexa(45);
obj.octal_to_hexa(-35);
obj.octal_to_hexa(63);
}
main();``````

#### Output

``````Octal : 16 Hexadecimal : E

Octal : 45 Hexadecimal : 25

Octal : -35 Hexadecimal : -1D

Octal : 63 Hexadecimal : 33
``````
``````#   Python 3 Program
#   Convert octal to hexadecimal number

class MyNumber :
digit = 0
result = ""
while (number != 0) :
digit = number % 16
if (digit < 0) :
digit = -digit

if (digit < 10) :
# 48 ASCII of '0'
result = chr((digit + 48)) + result
elif (digit >= 10) :
# 55 ASCII of '7'
result = chr((digit + 55)) + result

number = int(number / 16)

return result

def octal_to_decimal(self, number) :
result = 0
multiplier = 1
remainder = 0
while (number != 0) :
remainder = number % 10

result = (remainder * multiplier) + result
multiplier *= 8

number = int(number / 10)

return result

def octal_to_hexa(self, number) :
print("Octal : ", number, end = "")
flag = 0
if (number < 0) :
# When number is negative number
flag = 1
number = -number

number = self.octal_to_decimal(number)
if (flag == 1) :
result = "-"+ result

print(" Hexadecimal : ", result ,"\n\n", end = "")

def main() :
obj = MyNumber()
# Test Cases
obj.octal_to_hexa(16)
obj.octal_to_hexa(45)
obj.octal_to_hexa(-35)
obj.octal_to_hexa(63)

if __name__ == "__main__": main()``````

#### Output

``````Octal :  16 Hexadecimal :  E

Octal :  45 Hexadecimal :  25

Octal :  -35 Hexadecimal :  -1D

Octal :  63 Hexadecimal :  33
``````
``````#   Ruby Program
#   Convert octal to hexadecimal number

class MyNumber

digit = 0
result = ""
while (number != 0)

digit = number % 16
if (digit < 0)

digit = -digit
end
if (digit < 10)

# 48 ASCII of '0'
result = ((digit + 48)).chr + result
elsif (digit >= 10)

# 55 ASCII of '7'
result = ((digit + 55)).chr + result
end
number /= 16
end
return result
end
def octal_to_decimal(number)

result = 0
multiplier = 1
remainder = 0
while (number != 0)

remainder = number % 10

result = (remainder * multiplier) + result
multiplier *= 8

number /= 10
end
return result
end
def octal_to_hexa(number)

print("Octal  : ", number)
flag = 0
if (number < 0)

# When number is negative number
flag = 1
number = -number
end
number = self.octal_to_decimal(number)
if (flag == 1)

result = "-"+ result
end
print(" Hexadecimal  : ", result ,"\n\n")
end
end
def main()

obj = MyNumber.new()
# Test Cases
obj.octal_to_hexa(16)
obj.octal_to_hexa(45)
obj.octal_to_hexa(-35)
obj.octal_to_hexa(63)
end
main()``````

#### Output

``````Octal  : 16 Hexadecimal  : E

Octal  : 45 Hexadecimal  : 25

Octal  : -35 Hexadecimal  : -1D

Octal  : 63 Hexadecimal  : 33

``````
``````/*
Scala Program
*/
class MyNumber
{
def decimal_to_hexadecimal(num: Long): String = {
var digit: Long = 0;
var number: Long = num;
var result: String = "";
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of '0'
result = ""+ ((digit + 48)).toChar + result;
}
else if (digit >= 10)
{
//55 ASCII of '7'
result = ""+ ((digit + 55)).toChar + result;
}
number = (number / 16).toInt;
}
return result;
}
def octal_to_decimal(num: Long): Long = {
var result: Long = 0;
var multiplier: Long = 1;
var remainder: Long = 0;
var number: Long = num;
while (number != 0)
{
remainder = number % 10;

result = (remainder * multiplier) + result;
multiplier *= 8;

number = (number / 10).toInt;
}
return result;
}
def octal_to_hexa(num: Long): Unit = {
var number: Long = num;
print("Octal : " + number);
var flag: Int = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = octal_to_decimal(number);
if (flag == 1)
{
result = "-" + result;
}
print(" Hexadecimal : " + result + "\n\n");
}
}
object Main
{
def main(args: Array[String]): Unit = {
var obj: MyNumber = new MyNumber();
//Test Cases
obj.octal_to_hexa(16);
obj.octal_to_hexa(45);
obj.octal_to_hexa(-35);
obj.octal_to_hexa(63);
}
}``````

#### Output

``````Octal : 16 Hexadecimal : E

Octal : 45 Hexadecimal : 25

Octal : -35 Hexadecimal : -1D

Octal : 63 Hexadecimal : 33
``````
``````/*
Swift Program
*/
class MyNumber
{
func decimal_to_hexadecimal(_ num: Int) -> String
{
var digit: Int = 0;
var result: String = "";
var number: Int = num;
while (number != 0)
{
digit = number % 16;
if (digit < 0)
{
digit = -digit;
}
if (digit < 10)
{
//48 ASCII of "0"
result = String(UnicodeScalar(UInt8((digit + 48)))) + result;
}
else
if (digit >= 10)
{
//55 ASCII of "7"
result = String(UnicodeScalar(UInt8((digit + 55)))) + result;
}
number /= 16;
}
return result;
}
func octal_to_decimal(_ num:  Int) -> Int
{
var result: Int = 0;
var multiplier: Int = 1;
var remainder: Int = 0;
var number: Int = num;
while (number != 0)
{
remainder = number % 10;

result = (remainder * multiplier) + result;
multiplier *= 8;

number /= 10;
}
return result;
}
func octal_to_hexa(_ num:  Int)
{
var number: Int = num;
print("Octal : ", number, terminator: "");
var flag: Int = 0;
if (number < 0)
{
//When number is negative number
flag = 1;
number = -number;
}
number = self.octal_to_decimal(number);
if (flag == 1)
{
result = "-" + result;
}
print(" Hexadecimal : ", result ,"\n\n", terminator: "");
}
}
func main()
{
let obj: MyNumber = MyNumber();
//Test Cases
obj.octal_to_hexa(16);
obj.octal_to_hexa(45);
obj.octal_to_hexa(-35);
obj.octal_to_hexa(63);
}
main();``````

#### Output

``````Octal :  16 Hexadecimal :  E

Octal :  45 Hexadecimal :  25

Octal :  -35 Hexadecimal :  -1D

Octal :  63 Hexadecimal :  33
``````

## Comment

Please share your knowledge to improve code and content standard. Also submit your doubts, and test case. We improve by your feedback. We will try to resolve your query as soon as possible.

### New Comment

Kalkicode     629 Day ago
``You are right [ Webfalter] Thanks for the information. We are investigating what's wrong and will update as soon as possible.``
Webfalter     643 Day ago
``````Swift Program