Convert octal to hexadecimal number

Here given code implementation process.

//C Program
//Convert octal to hexadecimal number
#include <stdio.h>

//Reverse the resultant of hexadecimal
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;
	}
}
//Transform Decimal to hexadecimal
void decimal_to_hexadecimal(int number, char result[])
{
	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)
	{
		//add null char
		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];
	decimal_to_hexadecimal(number, result);
	if (flag == 1)
	{
		printf("Hexadecimal : -%s\n", result);
	}
	else
	{
		printf("Hexadecimal : %s\n", result);
	}
}
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
  Convert octal to hexadecimal number
*/
class MyNumber
{
	//Transform Decimal to hexadecimal
	public String decimal_to_hexadecimal(long number)
	{
		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);
		String result = decimal_to_hexadecimal(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
  Convert octal to hexadecimal number
*/
class MyNumber
{
	public:
		//Transform Decimal to hexadecimal
		string decimal_to_hexadecimal(long number)
		{
			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);
		string result = this->decimal_to_hexadecimal(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
  Convert octal to hexadecimal number
*/
class MyNumber
{
	//Transform Decimal to hexadecimal
	public String decimal_to_hexadecimal(long number)
	{
		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);
		String result = decimal_to_hexadecimal(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
  Convert octal to hexadecimal number
*/
class MyNumber
{
    //Transform Decimal to hexadecimal
    public  function decimal_to_hexadecimal($number)
    {
        $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);
        $result = $this->decimal_to_hexadecimal($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
  Convert octal to hexadecimal number
*/
class MyNumber
{
    //Transform Decimal to hexadecimal
    decimal_to_hexadecimal(number)
    {
        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);
        var result = this.decimal_to_hexadecimal(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 :
    # Transform Decimal to hexadecimal
    def decimal_to_hexadecimal(self, number) :
        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)
        result = self.decimal_to_hexadecimal(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

    # Transform Decimal to hexadecimal
    def decimal_to_hexadecimal(number)
    
        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)
        result = self.decimal_to_hexadecimal(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
  Convert octal to hexadecimal number
*/
class MyNumber
{
    //Transform Decimal to hexadecimal
    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);
        var result: String = decimal_to_hexadecimal(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
  Convert octal to hexadecimal number
*/
class MyNumber
{
    //Transform Decimal to hexadecimal
    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);
        var result: String = self.decimal_to_hexadecimal(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


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







© 2021, kalkicode.com, All rights reserved