Convert hexadecimal to decimal number

Here given code implementation process.

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

//Returns the power of given number of given size
int power(int number, int size)
{
	if (size == 0)
	{
		return 1;
	}
	if (number == 0)
	{
		return 0;
	}
	else
	{
		int result = number;
		for (int i = 2; i <= size; ++i)
		{
			//calculate power
			result = result *number;
		}
		return result;
	}
}
void hexa_to_decimal(char *hexa)
{
	long long decimal = 0;
	int digits = 0;
	int flag = 0;
	if (hexa[0] == '-')
	{
		flag = 1;
	}
	//Calculate the number of digits
	for (int i = flag; hexa[i] != '\0'; ++i)
	{
		digits++;
	}
	for (int i = flag; hexa[i] != '\0'; ++i, --digits)
	{
		if (hexa[i] >= '0' && hexa[i] <= '9')
		{
			//When hex digit, is a number
			decimal += (hexa[i] - 48) *power(16, digits - 1);
		}
		else if (hexa[i] >= 'a' && hexa[i] <= 'f')
		{
			//when of hexadecimal digit is an lowercase letter
			decimal += (hexa[i] - 87) *power(16, digits - 1);
		}
		else if (hexa[i] >= 'A' && hexa[i] <= 'F')
		{
			//when of hexadecimal digit is an uppercase letter
			decimal += (hexa[i] - 55) *power(16, digits - 1);
		}
		else
		{
			//invalid hexa decimal
			return;
		}
	}
	if (flag == 1)
	{
		decimal = -decimal;
	}
	printf("Hexa : %s  Decimal : %lld", hexa, decimal);
	printf("\n");
}
int main()
{
	//Test Cases
	hexa_to_decimal("A1");
	hexa_to_decimal("1A");
	hexa_to_decimal("-1A");
	hexa_to_decimal("f6");
	hexa_to_decimal("b1");
	hexa_to_decimal("da13");
	return 0;
}

Output

Hexa : A1  Decimal : 161
Hexa : 1A  Decimal : 26
Hexa : -1A  Decimal : -26
Hexa : f6  Decimal : 246
Hexa : b1  Decimal : 177
Hexa : da13  Decimal : 55827
/*
  C++ Program
  Convert hexadecimal to decimal number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public:
		//Returns the power of given number of given size
		int power(int number, int size)
		{
			if (size == 0)
			{
				return 1;
			}
			if (number == 0)
			{
				return 0;
			}
			else
			{
				int result = number;
				for (int i = 2; i <= size; ++i)
				{
					//calculate power
					result = result *number;
				}
				return result;
			}
		}
	void hexa_to_decimal(string hexa)
	{
		long decimal = 0;
		int digits = 0;
		int flag = 0;
		if (hexa[0] == '-')
		{
			flag = 1;
		}
		//Calculate the number of digits
		for (int i = flag; i < hexa.size(); ++i)
		{
			digits++;
		}
		for (int i = flag; i < hexa.size(); ++i, --digits)
		{
			if (hexa[i] >= '0' && hexa[i] <= '9')
			{
				//When hex digit, is a number
				decimal += (hexa[i] - 48) *this->power(16, digits - 1);
			}
			else if (hexa[i] >= 'a' && hexa[i] <= 'f')
			{
				//when of hexadecimal digit is an lowercase letter
				decimal += (hexa[i] - 87) *this->power(16, digits - 1);
			}
			else if (hexa[i] >= 'A' && hexa[i] <= 'F')
			{
				//when of hexadecimal digit is an uppercase letter
				decimal += (hexa[i] - 55) *this->power(16, digits - 1);
			}
			else
			{
				//invalid hexa decimal
				return;
			}
		}
		if (flag == 1)
		{
			decimal = -decimal;
		}
		cout << "Hexa : " << hexa << " Decimal : " << decimal;
		cout << "\n";
	}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Case
	obj.hexa_to_decimal("A1");
	obj.hexa_to_decimal("1A");
	obj.hexa_to_decimal("-1A");
	obj.hexa_to_decimal("f6");
	obj.hexa_to_decimal("b1");
	obj.hexa_to_decimal("da13");
	return 0;
}

Output

Hexa : A1 Decimal : 161
Hexa : 1A Decimal : 26
Hexa : -1A Decimal : -26
Hexa : f6 Decimal : 246
Hexa : b1 Decimal : 177
Hexa : da13 Decimal : 55827
/*
  Java Program
  Convert hexadecimal to decimal number
*/
public class MyNumber
{
 
  //Returns the power of given number of given size
  public int power(int number, int size)
  {
    if (size == 0)
    {
      return 1;
    }
    if (number == 0)
    {
      return 0;
    }
    else
    {
      int result = number;
      for (int i = 2; i <= size; ++i)
      {
        //calculate power
        result = result * number;
      }
      return result;
    }
  }
  public void hexa_to_decimal(String hexa)
  {
    long decimal = 0;

    int digits = 0;

    int flag = 0;

    if(hexa.charAt(0)=='-')
    {
      flag = 1;
    }

    //Calculate the number of digits
    for (int i = flag; i< hexa.length() ; ++i)
    {
      digits++;
    }

    for (int i = flag; i < hexa.length() ; ++i, --digits)
    {
      if (hexa.charAt(i) >= '0' && hexa.charAt(i) <= '9')
      {
        //When hex digit, is a number
        decimal += (hexa.charAt(i) - 48) * power(16, digits - 1);
      }
      else if (hexa.charAt(i) >= 'a' && hexa.charAt(i) <= 'f')
      {
        //when of hexadecimal digit is an lowercase letter
        decimal += (hexa.charAt(i) - 87) * power(16, digits - 1);
      }
      else if (hexa.charAt(i) >= 'A' && hexa.charAt(i) <= 'F')
      {
        //when of hexadecimal digit is an uppercase letter
        decimal += (hexa.charAt(i) - 55) * power(16, digits - 1);
      }
      else
      {
        
        //invalid hexa decimal
        return ;
      }
    }
    if(flag==1)
    {
      decimal = -decimal;
    }
    System.out.print("Hexa : "+hexa+" Decimal : "+decimal);
    System.out.print("\n");
  }
  public static void main(String[] args)
  {
    MyNumber obj = new MyNumber();
    //Test Case
    obj.hexa_to_decimal("A1");
    obj.hexa_to_decimal("1A");
    obj.hexa_to_decimal("-1A");
    obj.hexa_to_decimal("f6");
    obj.hexa_to_decimal("b1");
    obj.hexa_to_decimal("da13");
  }
}

Output

Hexa : A1 Decimal : 161
Hexa : 1A Decimal : 26
Hexa : -1A Decimal : -26
Hexa : f6 Decimal : 246
Hexa : b1 Decimal : 177
Hexa : da13 Decimal : 55827
/*
  C# Program
  Convert hexadecimal to decimal number
*/
using System;
public class MyNumber
{
	//Returns the power of given number of given size
	public int power(int number, int size)
	{
		if (size == 0)
		{
			return 1;
		}
		if (number == 0)
		{
			return 0;
		}
		else
		{
			int result = number;
			for (int i = 2; i <= size; i++)
			{
				//calculate power
				result = result * number;
			}
			return result;
		}
	}
	public void hexa_to_decimal(String hexa)
	{
		if (hexa.Length <= 0)
		{
			return ;
		}
		long result = 0;
		int digits = 0;
		int flag = 0;
		if (hexa[0] == '-')
		{
			flag = 1;
		}
		//Calculate the number of digits
		for (int i = flag; i < hexa.Length; i++)
		{
			digits++;
		}
		for (int i = flag; i < hexa.Length; i++, digits--)
		{
			if (hexa[i] >= '0' && hexa[i] <= '9')
			{
				//When hex digit, is a number
				result += (hexa[i] - 48) * power(16, digits - 1);
			}
			else if (hexa[i] >= 'a' && hexa[i] <= 'f')
			{
				//when of hexadecimal digit is an lowercase letter
				result += (hexa[i] - 87) * power(16, digits - 1);
			}
			else if (hexa[i] >= 'A' && hexa[i] <= 'F')
			{
				//when of hexadecimal digit is an uppercase letter
				result += (hexa[i] - 55) * power(16, digits - 1);
			}
			else
			{
				//invalid hexa decimal
				return;
			}
		}
		if (flag == 1)
		{
			result = -result;
		}
		Console.Write("Hexa : " + hexa + " Decimal : " + result);
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case
		obj.hexa_to_decimal("A1");
		obj.hexa_to_decimal("1A");
		obj.hexa_to_decimal("-1A");
		obj.hexa_to_decimal("f6");
		obj.hexa_to_decimal("b1");
		obj.hexa_to_decimal("da13");
	}
}

Output

Hexa : A1 Decimal : 161
Hexa : 1A Decimal : 26
Hexa : -1A Decimal : -26
Hexa : f6 Decimal : 246
Hexa : b1 Decimal : 177
Hexa : da13 Decimal : 55827
<?php
/*
  Php Program
  Convert hexadecimal to decimal number
*/
class MyNumber
{
	//Returns the power of given number of given size
	public 	function power($number, $size)
	{
		if ($size == 0)
		{
			return 1;
		}
		if ($number == 0)
		{
			return 0;
		}
		else
		{
			$result = $number;
			for ($i = 2; $i <= $size; ++$i)
			{
				//calculate power
				$result = $result *$number;
			}
			return $result;
		}
	}
	public 	function hexa_to_decimal($hexa)
	{
		if (strlen($hexa) <= 0)
		{
			return;
		}
		$decimal = 0;
		$digits = 0;
		$flag = 0;
		if ($hexa[0] == '-')
		{
			$flag = 1;
		}
		//Calculate the number of digits
		for ($i = $flag; $i < strlen($hexa); ++$i)
		{
			$digits++;
		}
		for ($i = $flag; $i < strlen($hexa); ++$i, --$digits)
		{
			if (ord($hexa[$i]) >= ord('0') && ord($hexa[$i]) <= ord('9'))
			{
				//When hex digit, is a number
				$decimal += (ord($hexa[$i]) - 48) *$this->power(16, $digits - 1);
			}
			else if (ord($hexa[$i]) >= ord('a') && ord($hexa[$i]) <= ord('f'))
			{
				//when of hexadecimal digit is an lowercase letter
				$decimal += (ord($hexa[$i]) - 87) *$this->power(16, $digits - 1);
			}
			else if (ord($hexa[$i]) >= ord('A') && ord($hexa[$i]) <= ord('F'))
			{
				//when of hexadecimal digit is an uppercase letter
				$decimal += (ord($hexa[$i]) - 55) *$this->power(16, $digits - 1);
			}
			else
			{
				return;
			}
		}
		if ($flag == 1)
		{
			$decimal = -$decimal;
		}
		echo("Hexa : ". $hexa ." Decimal : ". $decimal);
		echo("\n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Case
	$obj->hexa_to_decimal("A1");
	$obj->hexa_to_decimal("1A");
	$obj->hexa_to_decimal("-1A");
	$obj->hexa_to_decimal("f6");
	$obj->hexa_to_decimal("b1");
	$obj->hexa_to_decimal("da13");
}
main();

Output

Hexa : A1 Decimal : 161
Hexa : 1A Decimal : 26
Hexa : -1A Decimal : -26
Hexa : f6 Decimal : 246
Hexa : b1 Decimal : 177
Hexa : da13 Decimal : 55827
/*
  Node Js Program
  Convert hexadecimal to decimal number
*/
class MyNumber
{
	//Returns the power of given number of given size
	power(number, size)
	{
		if (size == 0)
		{
			return 1;
		}
		if (number == 0)
		{
			return 0;
		}
		else
		{
			var result = number;
			for (var i = 2; i <= size; ++i)
			{
				//calculate power
				result = result *number;
			}
			return result;
		}
	}
	hexa_to_decimal(hexa)
	{
		if (hexa.length <= 0)
		{
			return;
		}
		var decimal = 0;
		var digits = 0;
		var flag = 0;
		if (hexa[0] == '-')
		{
			flag = 1;
		}
		//Calculate the number of digits
		for (var i = flag; i < hexa.length; ++i)
		{
			digits++;
		}
		for (var i = flag; i < hexa.length; ++i, --digits)
		{
			if ((hexa[i]).charCodeAt(0) >= ('0').charCodeAt(0) && (hexa[i]).charCodeAt(0) <= ('9').charCodeAt(0))
			{
				//When hex digit, is a number
				decimal += ((hexa[i]).charCodeAt(0) - 48) *this.power(16, digits - 1);
			}
			else if ((hexa[i]).charCodeAt(0) >= ('a').charCodeAt(0) && (hexa[i]).charCodeAt(0) <= ('f').charCodeAt(0))
			{
				//when of hexadecimal digit is an lowercase letter
				decimal += ((hexa[i]).charCodeAt(0) - 87) *this.power(16, digits - 1);
			}
			else if ((hexa[i]).charCodeAt(0) >= ('A').charCodeAt(0) && (hexa[i]).charCodeAt(0) <= ('F').charCodeAt(0))
			{
				//when of hexadecimal digit is an uppercase letter
				decimal += ((hexa[i]).charCodeAt(0) - 55) *this.power(16, digits - 1);
			}
			else
			{
				return;
			}
		}
		if (flag == 1)
		{
			decimal = -decimal;
		}
		process.stdout.write("Hexa : " + hexa + " Decimal : " + decimal);
		process.stdout.write("\n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Case
	obj.hexa_to_decimal("A1");
	obj.hexa_to_decimal("1A");
	obj.hexa_to_decimal("-1A");
	obj.hexa_to_decimal("f6");
	obj.hexa_to_decimal("b1");
	obj.hexa_to_decimal("da13");
}
main();

Output

Hexa : A1 Decimal : 161
Hexa : 1A Decimal : 26
Hexa : -1A Decimal : -26
Hexa : f6 Decimal : 246
Hexa : b1 Decimal : 177
Hexa : da13 Decimal : 55827
#   Python 3 Program
#   Convert hexadecimal to decimal number

class MyNumber :
	# Returns the power of given number of given size
	def power(self, number, size) :
		if (size == 0) :
			return 1
		
		if (number == 0) :
			return 0
		else :
			result = number
			i = 2
			while (i <= size) :
				# calculate power
				result = result * number
				i += 1
			
			return result
		
	
	def hexa_to_decimal(self, hexa) :
		if (len(hexa) <= 0) :
			return
		
		decimal = 0
		digits = 0
		flag = 0
		if (hexa[0] == '-') :
			flag = 1
		
		# Calculate the number of digits
		i = flag
		while (i < len(hexa)) :
			digits += 1
			i += 1
		
		i = flag
		while (i < len(hexa)) :
			if (ord(hexa[i]) >= ord('0') and ord(hexa[i]) <= ord('9')) :
				# When hex digit, is a number
				decimal += (ord(hexa[i]) - 48) * self.power(16, digits - 1)
			elif (ord(hexa[i]) >= ord('a') and ord(hexa[i]) <= ord('f')) :
				# when of hexadecimal digit is an lowercase letter
				decimal += (ord(hexa[i]) - 87) * self.power(16, digits - 1)
			elif (ord(hexa[i]) >= ord('A') and ord(hexa[i]) <= ord('F')) :
				# when of hexadecimal digit is an uppercase letter
				decimal += (ord(hexa[i]) - 55) * self.power(16, digits - 1)
			else :
				return
			
			i += 1
			digits -= 1
		
		if (flag == 1) :
			decimal = -decimal
		
		print("Hexa : ", hexa ," Decimal : ", decimal, end = "")
		print("\n", end = "")
	

def main() :
	obj = MyNumber()
	# Test Case
	obj.hexa_to_decimal("A1")
	obj.hexa_to_decimal("1A")
	obj.hexa_to_decimal("-1A")
	obj.hexa_to_decimal("f6")
	obj.hexa_to_decimal("b1")
	obj.hexa_to_decimal("da13")


if __name__ == "__main__": main()

Output

Hexa :  A1  Decimal :  161
Hexa :  1A  Decimal :  26
Hexa :  -1A  Decimal :  -26
Hexa :  f6  Decimal :  246
Hexa :  b1  Decimal :  177
Hexa :  da13  Decimal :  55827
#   Ruby Program
#   Convert hexadecimal to decimal number

class MyNumber

	# Returns the power of given number of given size
	def power(number, size)
	
		if (size == 0)
		
			return 1
		end
		if (number == 0)
		
			return 0
		else
		
			result = number
			i = 2
			while (i <= size)
			
				# calculate power
				result = result * number
				i += 1
			end
			return result
		end
	end
	def hexa_to_decimal(hexa)
	
		if (hexa.length() <= 0)
		
			return
		end
		decimal = 0
		digits = 0
		flag = 0
		if (hexa[0] == '-')
		
			flag = 1
		end
		# Calculate the number of digits
		i = flag
		while (i < hexa.length())
		
			digits += 1
			i += 1
		end
		i = flag
		while (i < hexa.length())
		
			if ((hexa[i]).ord >= ('0').ord && (hexa[i]).ord <= ('9').ord)
			
				# When hex digit, is a number
				decimal += ((hexa[i]).ord - 48) * self.power(16, digits - 1)
			elsif ((hexa[i]).ord >= ('a').ord && (hexa[i]).ord <= ('f').ord)
			
				# when of hexadecimal digit is an lowercase letter
				decimal += ((hexa[i]).ord - 87) * self.power(16, digits - 1)
			elsif ((hexa[i]).ord >= ('A').ord && (hexa[i]).ord <= ('F').ord)
			
				# when of hexadecimal digit is an uppercase letter
				decimal += ((hexa[i]).ord - 55) * self.power(16, digits - 1)
			else
			
				return
			end
			i += 1
			digits -= 1
		end
		if (flag == 1)
		
			decimal = -decimal
		end
		print("Hexa  : ", hexa ," Decimal  : ", decimal)
		print("\n")
	end
end
def main()

	obj = MyNumber.new()
	# Test Case
	obj.hexa_to_decimal("A1")
	obj.hexa_to_decimal("1A")
	obj.hexa_to_decimal("-1A")
	obj.hexa_to_decimal("f6")
	obj.hexa_to_decimal("b1")
	obj.hexa_to_decimal("da13")
end
main()

Output

Hexa  : A1 Decimal  : 161
Hexa  : 1A Decimal  : 26
Hexa  : -1A Decimal  : -26
Hexa  : f6 Decimal  : 246
Hexa  : b1 Decimal  : 177
Hexa  : da13 Decimal  : 55827
/*
  Scala Program
  Convert hexadecimal to decimal number
*/
class MyNumber
{
	//Returns the power of given number of given size
	def power(number: Int, size: Int): Int = {
		if (size == 0)
		{
			return 1;
		}
		if (number == 0)
		{
			return 0;
		}
		else
		{
			var result: Int = number;
			var i: Int = 2;
			while (i <= size)
			{
				//calculate power
				result = result * number;
				i += 1;
			}
			return result;
		}
	}
	def hexa_to_decimal(hexa: String): Unit = {
		if (hexa.length() <= 0)
		{
			return;
		}
		var decimal: Long = 0;
		var digits: Int = 0;
		var flag: Int = 0;
		if (hexa(0) == '-')
		{
			flag = 1;
		}
		//Calculate the number of digits
		var i: Int = flag;
		while (i < hexa.length())
		{
			digits += 1;
			i += 1;
		}
		i = flag;
		while (i < hexa.length())
		{
			if (hexa(i) >= '0' && hexa(i) <= '9')
			{
				//When hex digit, is a number
				decimal += (hexa(i) - 48) * power(16, digits - 1);
			}
			else if (hexa(i) >= 'a' && hexa(i) <= 'f')
			{
				//when of hexadecimal digit is an lowercase letter
				decimal += (hexa(i) - 87) * power(16, digits - 1);
			}
			else if (hexa(i) >= 'A' && hexa(i) <= 'F')
			{
				//when of hexadecimal digit is an uppercase letter
				decimal += (hexa(i) - 55) * power(16, digits - 1);
			}
			else
			{
				return;
			}
			i += 1;
			digits -= 1;
		}
		if (flag == 1)
		{
			decimal = -decimal;
		}
		print("Hexa : " + hexa + " Decimal : " + decimal);
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.hexa_to_decimal("A1");
		obj.hexa_to_decimal("1A");
		obj.hexa_to_decimal("-1A");
		obj.hexa_to_decimal("f6");
		obj.hexa_to_decimal("b1");
		obj.hexa_to_decimal("da13");
	}
}

Output

Hexa : A1 Decimal : 161
Hexa : 1A Decimal : 26
Hexa : -1A Decimal : -26
Hexa : f6 Decimal : 246
Hexa : b1 Decimal : 177
Hexa : da13 Decimal : 55827
/*
  Swift Program
  Convert hexadecimal to decimal number
*/
class MyNumber
{
  //Returns the power of given number of given size
  func power(_ number: Int, _ size: Int) -> Int
  {
    if (size == 0)
    {
      return 1;
    }
    if (number == 0)
    {
      return 0;
    }
    else
    {
      var result: Int = number;
      var i: Int = 2;
      while (i <= size)
      {
        //calculate power
        result = result * number;
        i += 1;
      }
      return result;
    }
  }
  func hexa_to_decimal(_ data: String)
  {
        var hexa = Array(data);
    if (hexa.count <= 0)
    {
      return;
    }
    var decimal: Int = 0;
    var digits: Int = 0;
    var flag: Int = 0;
    if (hexa[0] == "-")
    {
      flag = 1;
    }
    //Calculate the number of digits
    var i: Int = flag;
    while (i < hexa.count)
    {
      digits += 1;
      i += 1;
    }
    i = flag;
    while (i < hexa.count)
    {
      if (hexa[i] >= "0" && hexa[i] <= "9")
      {
        //When hex digit, is a number
        decimal += (Int(UnicodeScalar(String(hexa[i]))!.value) - 48) * self.power(16, digits - 1);
      }
      else if (hexa[i] >= "a" && hexa[i] <= "f")
      {
        //when of hexadecimal digit is an lowercase letter
        decimal += (Int(UnicodeScalar(String(hexa[i]))!.value) - 87) * self.power(16, digits - 1);
      }
      else if (hexa[i] >= "A" && hexa[i] <= "F")
      {
        //when of hexadecimal digit is an uppercase letter
        decimal += (Int(UnicodeScalar(String(hexa[i]))!.value) - 55) * self.power(16, digits - 1);
      }
      else
      {
        return;
      }
      i += 1;
      digits -= 1;
    }
    if (flag == 1)
    {
      decimal = -decimal;
    }
    print("Hexa : ", data ," Decimal : ", decimal, terminator: "");
    print("\n", terminator: "");
  }
}
func main()
{
  let obj: MyNumber = MyNumber();
  //Test Case
  obj.hexa_to_decimal("A1");
  obj.hexa_to_decimal("1A");
  obj.hexa_to_decimal("-1A");
  obj.hexa_to_decimal("f6");
  obj.hexa_to_decimal("b1");
  obj.hexa_to_decimal("da13");
}
main();

Output

Hexa :  A1  Decimal :  161
Hexa :  1A  Decimal :  26
Hexa :  -1A  Decimal :  -26
Hexa :  f6  Decimal :  246
Hexa :  b1  Decimal :  177
Hexa :  da13  Decimal :  55827


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