Problem statement:- Program to Convert Binary to Hexadecimal.
Data requirement:-
Input Data:- binary
Output Data:-hexadecimal
Output Data:-hexadecimal
Program in C
Here is the source code of the C Program to convert Binary to Hexadecimal.
Code:
Code:
#include<stdio.h>
int
main ()
{
long int binary, hexadecimal = 0, j = 1, remain;
printf ("Enter binary number: ");
scanf ("%ld", &binary);
while (binary != 0)
{
remain = binary % 10;
hexadecimal = hexadecimal + remain * j;
j = j * 2;
binary = binary / 10;
}
printf ("Hexadecimal value: %lX", hexadecimal);
return 0;
}
main ()
{
long int binary, hexadecimal = 0, j = 1, remain;
printf ("Enter binary number: ");
scanf ("%ld", &binary);
while (binary != 0)
{
remain = binary % 10;
hexadecimal = hexadecimal + remain * j;
j = j * 2;
binary = binary / 10;
}
printf ("Hexadecimal value: %lX", hexadecimal);
return 0;
}
Input/Output:
Enter binary number: 10001111
Hexadecimal value: 8F
Hexadecimal value: 8F
Program in C++
Here is the source code of the C++ Program to convert Binary to Hexadecimal.
Code:
Code:
#include<iostream>
#include<string.h>
#include<cmath>
#include<sstream>
using namespace std;
int main()
{
string binary;
cout<<"Enter a binary number:";
getline(cin,binary);
if(binary.length()%4==1)
binary="000"+binary;
if(binary.length()%4==2)
binary="00"+binary;
if(binary.length()%4==3)
binary="0"+binary;
string hex="";
int len=binary.length()/4;
int i=0;
int j=0;
int k=4;
int decimal=0;
while(i++<len)
{
string st=binary.substr(j,4);
stringstream csinfo(st);
int bin=0;
csinfo>>bin;
decimal=0;
int temp=0,remainder=0;
while(bin!=0)
{
remainder=bin%10;
bin=bin/10;
decimal=decimal+remainder*pow(2,temp);
temp++;
}
if(decimal==15)
hex=hex+"F";
else if(decimal==14)
hex=hex+"E";
else if(decimal==13)
hex=hex+"D";
else if(decimal==12)
hex=hex+"C";
else if(decimal==11)
hex=hex+"B";
else if(decimal==10)
hex=hex+"A";
else
{
stringstream ss;
ss<<decimal;
string str;
ss>>str;
hex=hex+str;
}
j=j+4;
}
cout<<"Binary to HexaDecimal is "<<hex;
}
#include<cmath>
#include<sstream>
using namespace std;
int main()
{
string binary;
cout<<"Enter a binary number:";
getline(cin,binary);
if(binary.length()%4==1)
binary="000"+binary;
if(binary.length()%4==2)
binary="00"+binary;
if(binary.length()%4==3)
binary="0"+binary;
string hex="";
int len=binary.length()/4;
int i=0;
int j=0;
int k=4;
int decimal=0;
while(i++<len)
{
string st=binary.substr(j,4);
stringstream csinfo(st);
int bin=0;
csinfo>>bin;
decimal=0;
int temp=0,remainder=0;
while(bin!=0)
{
remainder=bin%10;
bin=bin/10;
decimal=decimal+remainder*pow(2,temp);
temp++;
}
if(decimal==15)
hex=hex+"F";
else if(decimal==14)
hex=hex+"E";
else if(decimal==13)
hex=hex+"D";
else if(decimal==12)
hex=hex+"C";
else if(decimal==11)
hex=hex+"B";
else if(decimal==10)
hex=hex+"A";
else
{
stringstream ss;
ss<<decimal;
string str;
ss>>str;
hex=hex+str;
}
j=j+4;
}
cout<<"Binary to HexaDecimal is "<<hex;
}
Input/Output:
Enter a binary number:110101111
Binary to HexaDecimal is 1AF
Binary to HexaDecimal is 1AF
Program in Java
Here is the source code of the Java Program to convert Binary to Hexadecimal.
Code:
Code:
import java.util.Scanner;
public class BinaryToHex {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a binary number :");
String binary=sc.nextLine();
if(binary.length()%4==1)
binary="000"+binary;
if(binary.length()%4==2)
binary="00"+binary;
if(binary.length()%4==3)
binary="0"+binary;
String hex="";
int len=binary.length()/4;
int i=0;
int j=0;
int k=4;
int decimal=0;
while(i++<len)
{
String st=binary.substring(j, k);
int bin=Integer.parseInt(st);
int temp=0,remainder=0;
decimal=0;
while(bin!=0)
{
remainder=bin%10;
bin=bin/10;
decimal=(int) (decimal+remainder*Math.pow(2, temp));
temp++;
}
if(decimal==15)
hex=hex+"F";
else if(decimal==14)
hex=hex+"E";
else if(decimal==13)
hex=hex+"D";
else if(decimal==12)
hex=hex+"C";
else if(decimal==11)
hex=hex+"B";
else if(decimal==10)
hex=hex+"A";
else {
String str=Integer.toString(decimal);
hex=hex+str;
}
j=k;
k=k+4;
}
System.out.println("Binary to HexaDecimal is "+hex);
sc.close();
}
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
System.out.println("Enter a binary number :");
String binary=sc.nextLine();
if(binary.length()%4==1)
binary="000"+binary;
if(binary.length()%4==2)
binary="00"+binary;
if(binary.length()%4==3)
binary="0"+binary;
String hex="";
int len=binary.length()/4;
int i=0;
int j=0;
int k=4;
int decimal=0;
while(i++<len)
{
String st=binary.substring(j, k);
int bin=Integer.parseInt(st);
int temp=0,remainder=0;
decimal=0;
while(bin!=0)
{
remainder=bin%10;
bin=bin/10;
decimal=(int) (decimal+remainder*Math.pow(2, temp));
temp++;
}
if(decimal==15)
hex=hex+"F";
else if(decimal==14)
hex=hex+"E";
else if(decimal==13)
hex=hex+"D";
else if(decimal==12)
hex=hex+"C";
else if(decimal==11)
hex=hex+"B";
else if(decimal==10)
hex=hex+"A";
else {
String str=Integer.toString(decimal);
hex=hex+str;
}
j=k;
k=k+4;
}
System.out.println("Binary to HexaDecimal is "+hex);
sc.close();
}
}
Input/Output:
Enter a binary number :
10111101
Binary to HexaDecimal is BD
10111101
Binary to HexaDecimal is BD
Program in Python
Here is the source code of the Python Program to convert Binary to Hexadecimal.
Code:
Code:
print("Enter a binary number:")
binary=input()
if(len(binary)%4==1):
binary="000"+binary
if(len(binary)%4==2):
binary="00"+binary
if(len(binary)%4==3):
binary="0"+binary
hex=""
len=int(len(binary)/4)
print("len:",len)
i=0
j=0
k=4
decimal=0
while(i<len):
st=binary[j:k]
bin=int(st)
temp=0
remainder=0
decimal=0
while(bin!=0):
remainder=bin%10
bin=bin//10
decimal=decimal+remainder*pow(2,temp)
temp=temp+1
if decimal==15:
hex=hex+"F"
elif decimal==14:
hex=hex+"E"
elif decimal==13:
hex=hex+"D"
elif decimal==12:
hex = hex + "C"
elif decimal==11:
hex=hex+"B"
elif decimal==10:
hex=hex+"A"
else:
hex=hex+str(decimal)
j=k
k=k+4
i=i+1
print("Binary to HexaDecimal is ",hex)
if(len(binary)%4==1):
binary="000"+binary
if(len(binary)%4==2):
binary="00"+binary
if(len(binary)%4==3):
binary="0"+binary
hex=""
len=int(len(binary)/4)
print("len:",len)
i=0
j=0
k=4
decimal=0
while(i<len):
st=binary[j:k]
bin=int(st)
temp=0
remainder=0
decimal=0
while(bin!=0):
remainder=bin%10
bin=bin//10
decimal=decimal+remainder*pow(2,temp)
temp=temp+1
if decimal==15:
hex=hex+"F"
elif decimal==14:
hex=hex+"E"
elif decimal==13:
hex=hex+"D"
elif decimal==12:
hex = hex + "C"
elif decimal==11:
hex=hex+"B"
elif decimal==10:
hex=hex+"A"
else:
hex=hex+str(decimal)
j=k
k=k+4
i=i+1
print("Binary to HexaDecimal is ",hex)
Input/Output:
Enter a binary number:
11010001
Binary to HexaDecimal is D1
11010001
Binary to HexaDecimal is D1
Most Recommend Questions:-
More Questions:-
0 Comments
Please do not Enter any spam link in the comment box