-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBinToHex.java
More file actions
51 lines (37 loc) · 729 Bytes
/
BinToHex.java
File metadata and controls
51 lines (37 loc) · 729 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
//convert Bin To Hex
import java.util.*;
public class BinToHex
{
public static void main(String args[])
{
int r1,r2,n,d=0,i=0;
Scanner sc=new Scanner(System.in);
System.out.println("Enter the Binary number= ");
n=sc.nextInt();
// conversion of Bin to Dec
while(true)
{
if(n==0)
break;
else
{
r1=n%10;
d+=r1*Math.pow(2,i);
n=n/10;
i++;
}
}
//System.out.println("Bin to Dec number= "+d);
n=d;
// Now Dec to Hex conversions Beging
char hex[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
String s=" ";
while(n>0)
{
r2=n%16;
s += s + hex[r2];
n=n/16;
}
System.out.print("Hex Number="+""+s+"");
}
}