Write
a C program to Convert Hexadecimal Number to Binary. or Write a
program to Convert Hexadecimal Number to Binary in C.
Code:
Code:
print("Enter a HexaDecimal number:")
hex=input()
binary=""
i=0
j=1
for i in range(0,len(hex)):
if hex[i]=='F' :
binary=binary+"1111"
elif hex[i]=="E":
binary=binary+"1110"
elif hex[i]=="D":
binary=binary+"1101"
elif hex[i]=="C":
binary=binary+"1100"
elif hex[i]=="B":
binary=binary+"1011"
elif hex[i]=="A":
binary=binary+"1010"
else:
st=hex[i:i+1]
decimal=0
temp=1
hexnum=int(st)
while hexnum!=0:
remainder=hexnum%2
hexnum=hexnum//2
decimal=decimal+remainder*temp
temp=temp*10
str1=str(decimal)
if len(str1)==3:
str1="0"+str1
if len(str1)==2:
str1="00"+str1
if len(str1)==1:
str1="000"+str1
binary=binary+str1
print("HexaDecimal to Binary is",binary)
Code:
#include<stdio.h>
void hextobin (char b[]);
void
main ()
{
int sizea,i;
printf ("Enter the array size:");
scanf ("%d", &sizea);
char hex[sizea+1];
printf("Enter Hexadecimal Number:");
for (i = 0; i < sizea+1; i++)
{
scanf ("%c", &hex[i]);
}
hextobin (hex);
}
void
hextobin (char hex[])
{
int i = 0;
while (hex[i])
{
switch (hex[i])
{
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 ("1000");
break;
case 'A':
printf ("1010");
break;
case 'a':
printf ("1010");
break;
case 'B':
printf ("1011");
break;
case 'b':
printf ("1011");
break;
case 'C':
printf ("1100");
break;
case 'c':
printf ("1100");
break;
case 'D':
printf ("1101");
break;
case 'd':
printf ("1101");
break;
case 'E':
printf ("1110");
break;
case 'e':
printf ("1110");
break;
case 'F':
printf ("1111");
break;
case 'f':
printf ("1111");
break;
}
i++;
}
}
Input/Output:
Enter the array size:2
Enter Hexadecimal Number:FF
11111111
Write
a C++ program to Convert Hexadecimal Number to Binary. or Write
a program to Convert Hexadecimal Number to Binary in C++.
Code:
#include<iostream>
#include<string.h>
#include<cmath>
#include<sstream>
using namespace std;
int main()
{
string hex;
cout<<"Enter a Hexadecimal number: ";
cin>>hex;
string binary="";
for(int i=0;i<hex.length();i++)
{
if(hex[i]=='F')
binary=binary+"1111";
else if(hex[i]=='E')
binary=binary+"1110";
else if(hex[i]=='D')
binary=binary+"1101";
else if(hex[i]=='C')
binary=binary+"1100";
else if(hex[i]=='B')
binary=binary+"1011";
else if(hex[i]=='A')
binary=binary+"1010";
else
{
string st=hex.substr(i,1);
stringstream csinfo(st);
int hexnum=0;
csinfo>>hexnum;
int decimal=0;
int remainder,temp=1;
while(hexnum!=0)
{
remainder=hexnum%2;
hexnum=hexnum/2;
decimal=decimal+remainder*temp;
temp=temp*10;
}
stringstream ss;
ss<<decimal;
string str;
ss>>str;
if(str.length()==3)
str="0"+str;
if(str.length()==2)
str="00"+str;
if(str.length()==1)
str="000"+str;
binary=binary+str;
}
}
cout<<"HexaDecimal to Binary is "<<binary;
}
Input/Output:
Enter a Hexadecimal number: ab
HexaDecimal to Binary is 10101011
Write
a JAVA program to Convert Hexadecimal Number to Binary. or Write
a program to Convert Hexadecimal Number to Binary in Java.
Code:
import java.util.Scanner;
public class HextoBinary {
public static void main(String[] args)
{
Scanner sc=new Scanner(System.in);
System.out.println("Enter a HexaDecimal number :");
String hex=sc.nextLine();
String binary="";
for(int i=0;i<hex.length();i++)
{
if(hex.charAt(i)=='F')
binary=binary+"1111";
else if(hex.charAt(i)=='E')
binary=binary+"1110";
else if(hex.charAt(i)=='D')
binary=binary+"1101";
else if(hex.charAt(i)=='C')
binary=binary+"1100";
else if(hex.charAt(i)=='B')
binary=binary+"1011";
else if(hex.charAt(i)=='A')
binary=binary+"1010";
else
{
String st=hex.substring(i,i+1);
int hexnum=Integer.parseInt(st);
int decimal=0;
int remainder,temp=1;
while(hexnum!=0)
{
remainder=hexnum%2;
hexnum=hexnum/2;
decimal=decimal+remainder*temp;
temp=temp*10;
}
String str=Integer.toString(decimal);
if(str.length()==3)
str="0"+str;
if(str.length()==2)
str="00"+str;
if(str.length()==1)
str="000"+str;
binary=binary+str;
}
}
System.out.println("HexaDecimal to Binary is "+binary);
sc.close();
}
}
Input/Output:
Enter a HexaDecimal number :
DD
HexaDecimal to Binary is 11011101
Write
a PYTHON to Convert Hexadecimal Number to Binary. or Write a
program to Convert Hexadecimal Number to Binary in Python
Code:
print("Enter a HexaDecimal number:")
hex=input()
binary=""
i=0
j=1
for i in range(0,len(hex)):
if hex[i]=='F' :
binary=binary+"1111"
elif hex[i]=="E":
binary=binary+"1110"
elif hex[i]=="D":
binary=binary+"1101"
elif hex[i]=="C":
binary=binary+"1100"
elif hex[i]=="B":
binary=binary+"1011"
elif hex[i]=="A":
binary=binary+"1010"
else:
st=hex[i:i+1]
decimal=0
temp=1
hexnum=int(st)
while hexnum!=0:
remainder=hexnum%2
hexnum=hexnum//2
decimal=decimal+remainder*temp
temp=temp*10
str1=str(decimal)
if len(str1)==3:
str1="0"+str1
if len(str1)==2:
str1="00"+str1
if len(str1)==1:
str1="000"+str1
binary=binary+str1
print("HexaDecimal to Binary is",binary)
Input/Output:
Enter a HexaDecimal number:
65BA
HexaDecimal to Binary is 0110010110111010
0 Comments
Please do not Enter any spam link in the comment box