Convert hexadecimal to binary number

Here given code implementation process.

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

void hexa_to_binary(char *hexa)
{
	printf("Hexa : %s  Binary : ", hexa);
	int flag = 0;
	if (hexa[0] == '-')
	{
		//When number is negative number
		flag = 1;
		printf("-");
	}
	for (int i = flag; hexa[i] != '\0'; ++i)
	{
		switch (hexa[i])
		{
			case 'a':
			case 'A':
				printf("1010");
				break;
			case 'b':
			case 'B':
				printf("1011");
				break;
			case 'c':
			case 'C':
				printf("1100");
				break;
			case 'd':
			case 'D':
				printf("1101");
				break;
			case 'e':
			case 'E':
				printf("1110");
				break;
			case 'f':
			case 'F':
				printf("1111");
				break;
			case '0':
				printf("0000");
				break;
			case '1':
				printf("0001");
				break;
			case '2':
				printf("0010");
				break;
			case '3':
				printf("0011");
				break;
			case '4':
				printf("0100");
				break;
			case '5':
				printf("0101");
				break;
			case '6':
				printf("0110");
				break;
			case '7':
				printf("0111");
				break;
			case '8':
				printf("1000");
				break;
			case '9':
				printf("1001");
				break;
			default:
				printf("%c Invaild\n", hexa[i]);
		}
	}
	printf("\n");
}
int main()
{
	//Test Case
	hexa_to_binary("9");
	hexa_to_binary("2D");
	hexa_to_binary("9A25E");
	hexa_to_binary("9a25e");
	hexa_to_binary("4F");
	hexa_to_binary("-2A");
	return 0;
}

Output

Hexa : 9  Binary : 1001
Hexa : 2D  Binary : 00101101
Hexa : 9A25E  Binary : 10011010001001011110
Hexa : 9a25e  Binary : 10011010001001011110
Hexa : 4F  Binary : 01001111
Hexa : -2A  Binary : -00101010
/*
  Java Program
  Convert hexadecimal to binary number
*/
public class MyNumber
{
 
  public void hexa_to_binary(String hexa)
  {
    System.out.print("Hexa : "+hexa+" Binary : ");

    int flag = 0;
    
    if (hexa.charAt(0) == '-')
    {
      //When number is negative number
      flag = 1;
      System.out.print("-");
    }
    for (int i = flag; i < hexa.length(); ++i)
    {
      switch (hexa.charAt(i))
      {
        case 'a':
        case 'A':
          System.out.print("1010");
          break;
        case 'b':
        case 'B':
          System.out.print("1011");
          break;
        case 'c':
        case 'C':
          System.out.print("1100");
          break;
        case 'd':
        case 'D':
          System.out.print("1101");
          break;
        case 'e':
        case 'E':
          System.out.print("1110");
          break;
        case 'f':
        case 'F':
          System.out.print("1111");
          break;
        case '0':
          System.out.print("0000");
          break;
        case '1':
          System.out.print("0001");
          break;
        case '2':
          System.out.print("0010");
          break;
        case '3':
          System.out.print("0011");
          break;
        case '4':
          System.out.print("0100");
          break;
        case '5':
          System.out.print("0101");
          break;
        case '6':
          System.out.print("0110");
          break;
        case '7':
          System.out.print("0111");
          break;
        case '8':
          System.out.print("1000");
          break;
        case '9':
          System.out.print("1001");
          break;
        default:
          System.out.print(""+hexa.charAt(i)+" Invaild\n");
      }
    }
    System.out.print("\n");
  }
  public static void main(String[] args)
  {
    MyNumber obj = new MyNumber();
    //Test Case
    obj.hexa_to_binary("9");
    obj.hexa_to_binary("2D");
    obj.hexa_to_binary("9A25E");
    obj.hexa_to_binary("9a25e");
    obj.hexa_to_binary("4F");
    obj.hexa_to_binary("-2A");
  }
}

Output

Hexa : 9  Binary : 1001
Hexa : 2D  Binary : 00101101
Hexa : 9A25E  Binary : 10011010001001011110
Hexa : 9a25e  Binary : 10011010001001011110
Hexa : 4F  Binary : 01001111
Hexa : -2A  Binary : -00101010
/*
  C++ Program
  Convert hexadecimal to binary number
*/
#include<iostream>

using namespace std;
class MyNumber
{
	public: 
    void hexa_to_binary(string hexa)
	{
		if (hexa.size() == 0)
		{
			return;
		}
		cout << "Hexa : " << hexa << " Binary : ";
		int flag = 0;
		if (hexa[0] == '-')
		{
			//When number is negative number
			flag = 1;
			cout << "-";
		}
		for (int i = flag; i < hexa.size(); ++i)
		{
			switch (hexa[i])
			{
				case 'a':
				case 'A':
					cout << "1010";
					break;
				case 'b':
				case 'B':
					cout << "1011";
					break;
				case 'c':
				case 'C':
					cout << "1100";
					break;
				case 'd':
				case 'D':
					cout << "1101";
					break;
				case 'e':
				case 'E':
					cout << "1110";
					break;
				case 'f':
				case 'F':
					cout << "1111";
					break;
				case '0':
					cout << "0000";
					break;
				case '1':
					cout << "0001";
					break;
				case '2':
					cout << "0010";
					break;
				case '3':
					cout << "0011";
					break;
				case '4':
					cout << "0100";
					break;
				case '5':
					cout << "0101";
					break;
				case '6':
					cout << "0110";
					break;
				case '7':
					cout << "0111";
					break;
				case '8':
					cout << "1000";
					break;
				case '9':
					cout << "1001";
					break;
				default:
					cout << "" << hexa[i] << " Invaild\n";
			}
		}
		cout << "\n";
	}
};
int main()
{
	MyNumber obj =  MyNumber();
	//Test Case
	obj.hexa_to_binary("9");
	obj.hexa_to_binary("2D");
	obj.hexa_to_binary("9A25E");
	obj.hexa_to_binary("9a25e");
	obj.hexa_to_binary("4F");
	obj.hexa_to_binary("-2A");
	return 0;
}

Output

Hexa : 9 Binary : 1001
Hexa : 2D Binary : 00101101
Hexa : 9A25E Binary : 10011010001001011110
Hexa : 9a25e Binary : 10011010001001011110
Hexa : 4F Binary : 01001111
Hexa : -2A Binary : -00101010
/*
  C# Program
  Convert hexadecimal to binary number
*/
using System;
public class MyNumber
{
	public void hexa_to_binary(String hexa)
	{
		if (hexa.Length == 0)
		{
			return;
		}
		Console.Write("Hexa : " + hexa + " Binary : ");
		int flag = 0;
		if (hexa[0] == '-')
		{
			//When number is negative number
			flag = 1;
			Console.Write("-");
		}
		for (int i = flag; i < hexa.Length; i++)
		{
			switch (hexa[i])
			{
				case 'a':
				case 'A':
					Console.Write("1010");
					break;
				case 'b':
				case 'B':
					Console.Write("1011");
					break;
				case 'c':
				case 'C':
					Console.Write("1100");
					break;
				case 'd':
				case 'D':
					Console.Write("1101");
					break;
				case 'e':
				case 'E':
					Console.Write("1110");
					break;
				case 'f':
				case 'F':
					Console.Write("1111");
					break;
				case '0':
					Console.Write("0000");
					break;
				case '1':
					Console.Write("0001");
					break;
				case '2':
					Console.Write("0010");
					break;
				case '3':
					Console.Write("0011");
					break;
				case '4':
					Console.Write("0100");
					break;
				case '5':
					Console.Write("0101");
					break;
				case '6':
					Console.Write("0110");
					break;
				case '7':
					Console.Write("0111");
					break;
				case '8':
					Console.Write("1000");
					break;
				case '9':
					Console.Write("1001");
					break;
				default:
					Console.Write("" + hexa[i] + " Invaild\n");
					break;
			}
		}
		Console.Write("\n");
	}
	public static void Main(String[] args)
	{
		MyNumber obj = new MyNumber();
		//Test Case
		obj.hexa_to_binary("9");
		obj.hexa_to_binary("2D");
		obj.hexa_to_binary("9A25E");
		obj.hexa_to_binary("9a25e");
		obj.hexa_to_binary("4F");
		obj.hexa_to_binary("-2A");
	}
}

Output

Hexa : 9 Binary : 1001
Hexa : 2D Binary : 00101101
Hexa : 9A25E Binary : 10011010001001011110
Hexa : 9a25e Binary : 10011010001001011110
Hexa : 4F Binary : 01001111
Hexa : -2A Binary : -00101010
<?php
/*
  Php Program
  Convert hexadecimal to binary number
*/
class MyNumber
{
	public 	function hexa_to_binary($hexa)
	{
		if (strlen($hexa) == 0)
		{
			return;
		}
		echo("Hexa : ". $hexa ." Binary : ");
		$flag = 0;
		if ($hexa[0] == '-')
		{
			//When number is negative number
			$flag = 1;
			echo("-");
		}
		for ($i = $flag; $i < strlen($hexa); ++$i)
		{
			switch ($hexa[$i])
			{
				case 'a':
				case 'A':
					echo("1010");
					break;
				case 'b':
				case 'B':
					echo("1011");
					break;
				case 'c':
				case 'C':
					echo("1100");
					break;
				case 'd':
				case 'D':
					echo("1101");
					break;
				case 'e':
				case 'E':
					echo("1110");
					break;
				case 'f':
				case 'F':
					echo("1111");
					break;
				case '0':
					echo("0000");
					break;
				case '1':
					echo("0001");
					break;
				case '2':
					echo("0010");
					break;
				case '3':
					echo("0011");
					break;
				case '4':
					echo("0100");
					break;
				case '5':
					echo("0101");
					break;
				case '6':
					echo("0110");
					break;
				case '7':
					echo("0111");
					break;
				case '8':
					echo("1000");
					break;
				case '9':
					echo("1001");
					break;
				default:
					echo("". $hexa[$i] ." Invaild\n");
			}
		}
		echo("\n");
	}
}

function main()
{
	$obj = new MyNumber();
	//Test Case
	$obj->hexa_to_binary("9");
	$obj->hexa_to_binary("2D");
	$obj->hexa_to_binary("9A25E");
	$obj->hexa_to_binary("9a25e");
	$obj->hexa_to_binary("4F");
	$obj->hexa_to_binary("-2A");
}
main();

Output

Hexa : 9 Binary : 1001
Hexa : 2D Binary : 00101101
Hexa : 9A25E Binary : 10011010001001011110
Hexa : 9a25e Binary : 10011010001001011110
Hexa : 4F Binary : 01001111
Hexa : -2A Binary : -00101010
/*
  Node Js Program
  Convert hexadecimal to binary number
*/
class MyNumber
{
	hexa_to_binary(hexa)
	{
		if (hexa.length == 0)
		{
			return;
		}
		process.stdout.write("Hexa : " + hexa + " Binary : ");
		var flag = 0;
		if (hexa[0] == '-')
		{
			//When number is negative number
			flag = 1;
			process.stdout.write("-");
		}
		for (var i = flag; i < hexa.length; ++i)
		{
			switch (hexa[i])
			{
				case 'a':
				case 'A':
					process.stdout.write("1010");
					break;
				case 'b':
				case 'B':
					process.stdout.write("1011");
					break;
				case 'c':
				case 'C':
					process.stdout.write("1100");
					break;
				case 'd':
				case 'D':
					process.stdout.write("1101");
					break;
				case 'e':
				case 'E':
					process.stdout.write("1110");
					break;
				case 'f':
				case 'F':
					process.stdout.write("1111");
					break;
				case '0':
					process.stdout.write("0000");
					break;
				case '1':
					process.stdout.write("0001");
					break;
				case '2':
					process.stdout.write("0010");
					break;
				case '3':
					process.stdout.write("0011");
					break;
				case '4':
					process.stdout.write("0100");
					break;
				case '5':
					process.stdout.write("0101");
					break;
				case '6':
					process.stdout.write("0110");
					break;
				case '7':
					process.stdout.write("0111");
					break;
				case '8':
					process.stdout.write("1000");
					break;
				case '9':
					process.stdout.write("1001");
					break;
				default:
					process.stdout.write("" + hexa[i] + " Invaild\n");
			}
		}
		process.stdout.write("\n");
	}
}

function main(args)
{
	var obj = new MyNumber();
	//Test Case
	obj.hexa_to_binary("9");
	obj.hexa_to_binary("2D");
	obj.hexa_to_binary("9A25E");
	obj.hexa_to_binary("9a25e");
	obj.hexa_to_binary("4F");
	obj.hexa_to_binary("-2A");
}
main();

Output

Hexa : 9 Binary : 1001
Hexa : 2D Binary : 00101101
Hexa : 9A25E Binary : 10011010001001011110
Hexa : 9a25e Binary : 10011010001001011110
Hexa : 4F Binary : 01001111
Hexa : -2A Binary : -00101010
#   Python 3 Program
#   Convert hexadecimal to binary number

class MyNumber :
  def hexa_to_binary(self, hexa) :
    if (len(hexa) == 0) :
      return
    
    print("Hexa : ", hexa ," Binary : ", end = "")
    flag = 0
    if (hexa[0] == '-') :
      # When number is negative number
      flag = 1
      print("-", end = "")
    
    i = flag
    while (i < len(hexa)) :
      if (hexa[i] == 'a' or hexa[i] == 'A') :
        print("1010", end = "")
      
      elif (hexa[i] == 'b' or hexa[i] == 'B') :
        print("1011", end = "")
      
      elif (hexa[i] == 'c' or hexa[i] == 'C') :
        print("1100", end = "")
      
      elif (hexa[i] == 'd' or hexa[i] == 'D') :
        print("1101", end = "")

      elif (hexa[i] == 'e' or hexa[i] == 'E') :
        print("1110", end = "")

      elif (hexa[i] == 'f' or hexa[i] == 'F') :
        print("1111", end = "")

      elif (hexa[i] == '0') :
        print("0000", end = "")

      elif (hexa[i] == '1') :
        print("0001", end = "")

      elif (hexa[i] == '2') :
        print("0010", end = "")

      elif (hexa[i] == '3') :
        print("0011", end = "")

      elif (hexa[i] == '4') :
        print("0100", end = "")

      elif (hexa[i] == '5') :
        print("0101", end = "")

      elif (hexa[i] == '6') :
        print("0110", end = "")

      elif (hexa[i] == '7') :
        print("0111", end = "")

      elif (hexa[i] == '8') :
        print("1000", end = "")

      elif (hexa[i] == '9') :
        print("1001", end = "")

      else :
        print("", hexa[i] ," Invaild\n", end = "")

      i += 1

    print("\n", end = "")


def main() :
  obj = MyNumber()
  # Test Case
  obj.hexa_to_binary("9")
  obj.hexa_to_binary("2D")
  obj.hexa_to_binary("9A25E")
  obj.hexa_to_binary("9a25e")
  obj.hexa_to_binary("4F")
  obj.hexa_to_binary("-2A")


if __name__ == "__main__": main()

Output

Hexa :  9  Binary : 1001
Hexa :  2D  Binary : 00101101
Hexa :  9A25E  Binary : 10011010001001011110
Hexa :  9a25e  Binary : 10011010001001011110
Hexa :  4F  Binary : 01001111
Hexa :  -2A  Binary : -00101010
#   Ruby Program
#   Convert hexadecimal to binary number

class MyNumber

  def hexa_to_binary(hexa)
    
    if (hexa.length() == 0)
      return
    end
    print("Hexa  : ", hexa ," Binary  : ")
    flag = 0
    if (hexa[0] == '-')
    
      # When number is negative number
      flag = 1
      print("-")
    end
    i = flag
    while (i < hexa.length())
    
      if (hexa[i] == 'a' || hexa[i] == 'A') 
        print("1010")
      
      elsif (hexa[i] == 'b' || hexa[i] == 'B') 
        print("1011")
      
      elsif (hexa[i] == 'c' || hexa[i] == 'C') 
        print("1100")
      
      elsif (hexa[i] == 'd' || hexa[i] == 'D') 
        print("1101")

      elsif (hexa[i] == 'e' || hexa[i] == 'E') 
        print("1110")

      elsif (hexa[i] == 'f' || hexa[i] == 'F') 
        print("1111")

      elsif (hexa[i] == '0') 
        print("0000")

      elsif (hexa[i] == '1') 
        print("0001")

      elsif (hexa[i] == '2') 
        print("0010")

      elsif (hexa[i] == '3') 
        print("0011")

      elsif (hexa[i] == '4') 
        print("0100")

      elsif (hexa[i] == '5') 
        print("0101")

      elsif (hexa[i] == '6') 
        print("0110")

      elsif (hexa[i] == '7') 
        print("0111")

      elsif (hexa[i] == '8') 
        print("1000")

      elsif (hexa[i] == '9') 
        print("1001")

      else 
        print( hexa[i] ," Invaild\n")
      end
      i+=1
    end
    print("\n")
  end
end
def main()

  obj = MyNumber.new()
  # Test Case
  obj.hexa_to_binary("9")
  obj.hexa_to_binary("2D")
  obj.hexa_to_binary("9A25E")
  obj.hexa_to_binary("9a25e")
  obj.hexa_to_binary("4F")
  obj.hexa_to_binary("-2A")
end
main()

Output

Hexa  : 9 Binary  : 1001
Hexa  : 2D Binary  : 00101101
Hexa  : 9A25E Binary  : 10011010001001011110
Hexa  : 9a25e Binary  : 10011010001001011110
Hexa  : 4F Binary  : 01001111
Hexa  : -2A Binary  : -00101010
/*
  Scala Program
  Convert hexadecimal to binary number
*/
class MyNumber
{
	def hexa_to_binary(hexa: String): Unit = {
		if (hexa.length() == 0)
		{
			return;
		}
		print("Hexa : " + hexa + " Binary : ");
		var flag: Int = 0;
		if (hexa(0) == '-')
		{
			//When number is negative number
			flag = 1;
			print("-");
		}
		var i: Int = flag;
		while (i < hexa.length())
		{
			if (hexa(i) == 'a' || hexa(i) == 'A')
			{
				print("1010");
			}
			else if (hexa(i) == 'b' || hexa(i) == 'B')
			{
				print("1011");
			}
			else if (hexa(i) == 'c' || hexa(i) == 'C')
			{
				print("1100");
			}
			else if (hexa(i) == 'd' || hexa(i) == 'D')
			{
				print("1101");
			}
			else if (hexa(i) == 'e' || hexa(i) == 'E')
			{
				print("1110");
			}
			else if (hexa(i) == 'f' || hexa(i) == 'F')
			{
				print("1111");
			}
			else if (hexa(i) == '0')
			{
				print("0000");
			}
			else if (hexa(i) == '1')
			{
				print("0001");
			}
			else if (hexa(i) == '2')
			{
				print("0010");
			}
			else if (hexa(i) == '3')
			{
				print("0011");
			}
			else if (hexa(i) == '4')
			{
				print("0100");
			}
			else if (hexa(i) == '5')
			{
				print("0101");
			}
			else if (hexa(i) == '6')
			{
				print("0110");
			}
			else if (hexa(i) == '7')
			{
				print("0111");
			}
			else if (hexa(i) == '8')
			{
				print("1000");
			}
			else if (hexa(i) == '9')
			{
				print("1001");
			}
			else
			{
				print("" + hexa(i) + " Invaild\n");
			}
			i += 1;
		}
		print("\n");
	}
}
object Main
{
	def main(args: Array[String]): Unit = {
		var obj: MyNumber = new MyNumber();
		//Test Case
		obj.hexa_to_binary("9");
		obj.hexa_to_binary("2D");
		obj.hexa_to_binary("9A25E");
		obj.hexa_to_binary("9a25e");
		obj.hexa_to_binary("4F");
		obj.hexa_to_binary("-2A");
	}
}

Output

Hexa : 9 Binary : 1001
Hexa : 2D Binary : 00101101
Hexa : 9A25E Binary : 10011010001001011110
Hexa : 9a25e Binary : 10011010001001011110
Hexa : 4F Binary : 01001111
Hexa : -2A Binary : -00101010
/*
  Swift Program
  Convert hexadecimal to binary number
*/
class MyNumber
{
  func hexa_to_binary(_ data: String)
  {
    if (data.count == 0)
    {
      return;
    }
    
    print("Hexa : ", data, " Binary : ", terminator: "");
    var hexa = Array(data);
    var flag: Int = 0;
    if (hexa[0] == "-")
    {
      //When number is negative number
      flag = 1;
      print("-", terminator: "");
    }
    var i: Int = flag;
    while (i < hexa.count)
    {
      if (hexa[i] == "a" || hexa[i] == "A")
      {
        print("1010", terminator: "");
      }
      else if (hexa[i] == "b" || hexa[i] == "B")
      {
        print("1011", terminator: "");
      }
      else if (hexa[i] == "c" || hexa[i] == "C")
      {
        print("1100", terminator: "");
      }
      else if (hexa[i] == "d" || hexa[i] == "D")
      {
        print("1101", terminator: "");
      }
      else if (hexa[i] == "e" || hexa[i] == "E")
      {
        print("1110", terminator: "");
      }
      else if (hexa[i] == "f" || hexa[i] == "F")
      {
        print("1111", terminator: "");
      }
      else if (hexa[i] == "0")
      {
        print("0000", terminator: "");
      }
      else if (hexa[i] == "1")
      {
        print("0001", terminator: "");
      }
      else if (hexa[i] == "2")
      {
        print("0010", terminator: "");
      }
      else if (hexa[i] == "3")
      {
        print("0011", terminator: "");
      }
      else if (hexa[i] == "4")
      {
        print("0100", terminator: "");
      }
      else if (hexa[i] == "5")
      {
        print("0101", terminator: "");
      }
      else if (hexa[i] == "6")
      {
        print("0110", terminator: "");
      }
      else if (hexa[i] == "7")
      {
        print("0111", terminator: "");
      }
      else if (hexa[i] == "8")
      {
        print("1000", terminator: "");
      }
      else if (hexa[i] == "9")
      {
        print("1001", terminator: "");
      }
      else
      {
        print(hexa[i] , " Invaild");
      }
  
      i += 1;
    }
    print("\n", terminator: "");
  }
}
func main()
{
  let obj: MyNumber = MyNumber();
  //Test Case
  obj.hexa_to_binary("9");
  obj.hexa_to_binary("2D");
  obj.hexa_to_binary("9A25E");
  obj.hexa_to_binary("9a25e");
  obj.hexa_to_binary("4F");
  obj.hexa_to_binary("-2A");
}
main();

Output

Hexa :  9  Binary : 1001
Hexa :  2D  Binary : 00101101
Hexa :  9A25E  Binary : 10011010001001011110
Hexa :  9a25e  Binary : 10011010001001011110
Hexa :  4F  Binary : 01001111
Hexa :  -2A  Binary : -00101010


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