Convert decimal to hexadecimal number

Here given code implementation process.

//C Program
//Convert decimal 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;
	}
}
void hexadecimal(int number, char result[])
{
	int flag = 0;
	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);
	}
}
void decimal_to_hexa(int number)
{
	char output[32];
	int flag = 0;
	printf("Decimal : %d ", number);
	if (number < 0)
	{
		number = -number;
		flag = 1;
	}
	//Convert decimal to hexa decimal
	hexadecimal(number, output);
	if (flag == 0)
	{
		printf(" Hexadecimal : %s \n", output);
	}
	else
	{
		//When number is negative number
		printf(" Hexadecimal : -%s \n", output);
	}
}
int main()
{
	//Test Case
	decimal_to_hexa(16);
	decimal_to_hexa(-16);
	decimal_to_hexa(143);
	decimal_to_hexa(148);
	decimal_to_hexa(128);
	decimal_to_hexa(14);
	decimal_to_hexa(-17);
	return 0;
}
    

Output

Decimal : 16  Hexadecimal : 10
Decimal : -16  Hexadecimal : -10
Decimal : 143  Hexadecimal : 8F
Decimal : 148  Hexadecimal : 94
Decimal : 128  Hexadecimal : 80
Decimal : 14  Hexadecimal : E
Decimal : -17  Hexadecimal : -11
/*
  Java Program
  Convert decimal to hexadecimal number
*/
public class MyNumber
{
	//Convert decimal to hexadecimal
	public String hexa_decimal(long number)
	{
		String result = "";
		long digit = 0;
		int index = 0;
		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;
			}
			index++;
			number /= 16;
		}
		return result;
	}
	public void decimal_to_hexa(long number)
	{
		System.out.print("Decimal :  " + number);
		boolean flag = false;
		if (number < 0)
		{
			flag = true;
		}
		//Convert decimal to hexa decimal
		String output = hexa_decimal(number);
		if (flag == true)
		{
			//When negative element exist
			output = "-" + output;
		}
		System.out.print(" Hexadecimal : " + output + " \n");
	}
	public static void main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.decimal_to_hexa(16);
		obj.decimal_to_hexa(-16);
		obj.decimal_to_hexa(143);
		obj.decimal_to_hexa(148);
		obj.decimal_to_hexa(128);
		obj.decimal_to_hexa(14);
		obj.decimal_to_hexa(-17);
	}
}
    

Output

Decimal : 16  Hexadecimal : 10
Decimal : -16  Hexadecimal : -10
Decimal : 143  Hexadecimal : 8F
Decimal : 148  Hexadecimal : 94
Decimal : 128  Hexadecimal : 80
Decimal : 14  Hexadecimal : E
Decimal : -17  Hexadecimal : -11
/*
  C++ Program
  Convert decimal to hexadecimal number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public:
		//Convert decimal to hexadecimal
		string hexa_decimal(long number)
		{
			string result = "";
			long digit = 0;
			int index = 0;
			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;
				}
				index++;
				number /= 16;
			}
			return result;
		}
	void decimal_to_hexa(long number)
	{
		cout << "Decimal : " << number;
		bool flag = false;
		if (number < 0)
		{
			flag = true;
		}
		//Convert decimal to hexa decimal
		string output = this->hexa_decimal(number);
		if (flag == true)
		{
			//When negative element exist
			output = "-" + output;
		}
		cout << " Hexadecimal : " << output << " \n";
	}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Cases
	obj.decimal_to_hexa(16);
	obj.decimal_to_hexa(-16);
	obj.decimal_to_hexa(143);
	obj.decimal_to_hexa(148);
	obj.decimal_to_hexa(128);
	obj.decimal_to_hexa(14);
	obj.decimal_to_hexa(-17);
	return 0;
}
    

Output

Decimal : 16 Hexadecimal : 10
Decimal : -16 Hexadecimal : -10
Decimal : 143 Hexadecimal : 8F
Decimal : 148 Hexadecimal : 94
Decimal : 128 Hexadecimal : 80
Decimal : 14 Hexadecimal : E
Decimal : -17 Hexadecimal : -11
/*
  C# Program
  Convert decimal to hexadecimal number
*/
using System;
public class MyNumber
{
	//Convert decimal to hexadecimal
	public String hexa_decimal(long number)
	{
		String result = "";
		long digit = 0;
		int index = 0;
		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;
			}
			index++;
			number /= 16;
		}
		return result;
	}
	public void decimal_to_hexa(long number)
	{
		Console.Write("Decimal : " + number);
		Boolean flag = false;
		if (number < 0)
		{
			flag = true;
		}
		//Convert decimal to hexa decimal
		String output = hexa_decimal(number);
		if (flag == true)
		{
			//When negative element exist
			output = "-" + output;
		}
		Console.Write(" Hexadecimal : " + output + " \n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Cases
		obj.decimal_to_hexa(16);
		obj.decimal_to_hexa(-16);
		obj.decimal_to_hexa(143);
		obj.decimal_to_hexa(148);
		obj.decimal_to_hexa(128);
		obj.decimal_to_hexa(14);
		obj.decimal_to_hexa(-17);
	}
}
    

Output

Decimal : 16 Hexadecimal : 10
Decimal : -16 Hexadecimal : -10
Decimal : 143 Hexadecimal : 8F
Decimal : 148 Hexadecimal : 94
Decimal : 128 Hexadecimal : 80
Decimal : 14 Hexadecimal : E
Decimal : -17 Hexadecimal : -11
<?php
/*
  Php Program
  Convert decimal to hexadecimal number
*/
class MyNumber
{
	//Convert decimal to hexadecimal
	public 	function hexa_decimal($number)
	{
		$result = "";
		$digit = 0;
		$index = 0;
		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;
			}
			$index++;
			$number = intval($number / 16);
		}
		return $result;
	}
	public 	function decimal_to_hexa($number)
	{
		echo("Decimal : ". $number);
		$flag = false;
		if ($number < 0)
		{
			$flag = true;
		}
		//Convert decimal to hexa decimal
		$output = $this->hexa_decimal($number);
		if ($flag == true)
		{
			//When negative element exist
			$output = "-". $output;
		}
		echo(" Hexadecimal : ". $output ." \n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Cases
	$obj->decimal_to_hexa(16);
	$obj->decimal_to_hexa(-16);
	$obj->decimal_to_hexa(143);
	$obj->decimal_to_hexa(148);
	$obj->decimal_to_hexa(128);
	$obj->decimal_to_hexa(14);
	$obj->decimal_to_hexa(-17);
}
main();
    

Output

Decimal : 16 Hexadecimal : 10
Decimal : -16 Hexadecimal : -10
Decimal : 143 Hexadecimal : 8F
Decimal : 148 Hexadecimal : 94
Decimal : 128 Hexadecimal : 80
Decimal : 14 Hexadecimal : E
Decimal : -17 Hexadecimal : -11
/*
  Node Js Program
  Convert decimal to hexadecimal number
*/
class MyNumber
{
	//Convert decimal to hexadecimal
	hexa_decimal(number)
	{
		var result = "";
		var digit = 0;
		var index = 0;
		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;
			}
			index++;
			number = parseInt(number / 16);
		}
		return result;
	}
	decimal_to_hexa(number)
	{
		process.stdout.write("Decimal : " + number);
		var flag = false;
		if (number < 0)
		{
			flag = true;
		}
		//Convert decimal to hexa decimal
		var output = this.hexa_decimal(number);
		if (flag == true)
		{
			//When negative element exist
			output = "-" + output;
		}
		process.stdout.write(" Hexadecimal : " + output + " \n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Cases
	obj.decimal_to_hexa(16);
	obj.decimal_to_hexa(-16);
	obj.decimal_to_hexa(143);
	obj.decimal_to_hexa(148);
	obj.decimal_to_hexa(128);
	obj.decimal_to_hexa(14);
	obj.decimal_to_hexa(-17);
}
main();
    

Output

Decimal : 16 Hexadecimal : 10
Decimal : -16 Hexadecimal : -10
Decimal : 143 Hexadecimal : 8F
Decimal : 148 Hexadecimal : 94
Decimal : 128 Hexadecimal : 80
Decimal : 14 Hexadecimal : E
Decimal : -17 Hexadecimal : -11
#   Python 3 Program
#   Convert decimal to hexadecimal number

class MyNumber :
	# Convert decimal to hexadecimal
	def hexa_decimal(self, number) :
		result = ""
		digit = 0
		index = 0
		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
			
			index += 1
			number = int(number / 16)
		
		return result
	
	def decimal_to_hexa(self, number) :
		print("Decimal : ", number, end = "")
		flag = False
		if (number < 0) :
			flag = True
			number = -number
		
		# Convert decimal to hexa decimal
		output = self.hexa_decimal(number)
		if (flag == True) :
			# When negative element exist
			output = "-"+ output
		
		print(" Hexadecimal : ", output ," \n", end = "")
	

def main() :
	obj = MyNumber()
	# Test Cases
	obj.decimal_to_hexa(16)
	obj.decimal_to_hexa(-16)
	obj.decimal_to_hexa(143)
	obj.decimal_to_hexa(148)
	obj.decimal_to_hexa(128)
	obj.decimal_to_hexa(14)
	obj.decimal_to_hexa(-17)


if __name__ == "__main__": main()
    

Output

Decimal :  16 Hexadecimal :  10
Decimal :  -16 Hexadecimal :  -10
Decimal :  143 Hexadecimal :  8F
Decimal :  148 Hexadecimal :  94
Decimal :  128 Hexadecimal :  80
Decimal :  14 Hexadecimal :  E
Decimal :  -17 Hexadecimal :  -11
#   Ruby Program
#   Convert decimal to hexadecimal number

class MyNumber

	# Convert decimal to hexadecimal
	def hexa_decimal(number)
	
		result = ""
		digit = 0
		index = 0
		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
			index += 1
			number /= 16
		end
		return result
	end
	def decimal_to_hexa(number)
	
		print("Decimal  : ", number)
		flag = false
		if (number < 0)
		
			flag = true
			number = -number
		end
		# Convert decimal to hexa decimal
		output = self.hexa_decimal(number)
		if (flag == true)
		
			# When negative element exist
			output = "-"+ output
		end
		print(" Hexadecimal  : ", output ," \n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Cases
	obj.decimal_to_hexa(16)
	obj.decimal_to_hexa(-16)
	obj.decimal_to_hexa(143)
	obj.decimal_to_hexa(148)
	obj.decimal_to_hexa(128)
	obj.decimal_to_hexa(14)
	obj.decimal_to_hexa(-17)
end
main()
    

Output

Decimal  : 16 Hexadecimal  : 10 
Decimal  : -16 Hexadecimal  : -10 
Decimal  : 143 Hexadecimal  : 8F 
Decimal  : 148 Hexadecimal  : 94 
Decimal  : 128 Hexadecimal  : 80 
Decimal  : 14 Hexadecimal  : E 
Decimal  : -17 Hexadecimal  : -11 
/*
  Scala Program
  Convert decimal to hexadecimal number
*/
class MyNumber
{
	//Convert decimal to hexadecimal
	def hexa_decimal(num: Long): String = {
		var result: String = "";
		var digit: Long = 0;
		var index: Int = 0;
      	var number: Long = num;
		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);
			}
			index += 1;
			number = (number / 16).toInt;
		}
		return result;
	}
	def decimal_to_hexa(num: Long): Unit = {
      	var number: Long = num;
		print("Decimal : " + number);
		var flag: Boolean = false;
		if (number < 0)
		{
			flag = true;
			number = -number;
		}
		//Convert decimal to hexa decimal
		var output: String = hexa_decimal(number);
		if (flag == true)
		{
			//When negative element exist
			output = "-" + output;
		}
		print(" Hexadecimal : " + output + " \n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Cases
		obj.decimal_to_hexa(16);
		obj.decimal_to_hexa(-16);
		obj.decimal_to_hexa(143);
		obj.decimal_to_hexa(148);
		obj.decimal_to_hexa(128);
		obj.decimal_to_hexa(14);
		obj.decimal_to_hexa(-17);
	}
}
    

Output

Decimal : 16 Hexadecimal : 10
Decimal : -16 Hexadecimal : -10
Decimal : 143 Hexadecimal : 8F
Decimal : 148 Hexadecimal : 94
Decimal : 128 Hexadecimal : 80
Decimal : 14 Hexadecimal : E
Decimal : -17 Hexadecimal : -11
/*
  Swift Program
  Convert decimal to hexadecimal number
*/
class MyNumber
{
	//Convert decimal to hexadecimal
	func hexa_decimal(_ number: inout Int) -> String
	{
		var result: String = "";
		var digit: Int = 0;
		var index: Int = 0;
		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;
			}
			index += 1;
			number /= 16;
		}
		return result;
	}
	func decimal_to_hexa(_ num:  Int)
	{
      	var number : Int = num;
		print("Decimal : ", number, terminator: "");
		var flag: Bool = false;
		if (number < 0)
		{
			flag = true;
			number = -number;
		}
		//Convert decimal to hexa decimal
		var output: String = self.hexa_decimal(&number);
		if (flag == true)
		{
			//When negative element exist
			output = "-" + output;
		}
		print(" Hexadecimal : ", output ," \n", terminator: "");
	}
}
func main()
{
	let obj: MyNumber = MyNumber();
	//Test Cases
	obj.decimal_to_hexa(16);
	obj.decimal_to_hexa(-16);
	obj.decimal_to_hexa(143);
	obj.decimal_to_hexa(148);
	obj.decimal_to_hexa(128);
	obj.decimal_to_hexa(14);
	obj.decimal_to_hexa(-17);
}
main();
    

Output

Decimal :  16 Hexadecimal :  10
Decimal :  -16 Hexadecimal :  -10
Decimal :  143 Hexadecimal :  8F
Decimal :  148 Hexadecimal :  94
Decimal :  128 Hexadecimal :  80
Decimal :  14 Hexadecimal :  E
Decimal :  -17 Hexadecimal :  -11


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