-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBinary.java
73 lines (66 loc) · 2.17 KB
/
Binary.java
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
import java.io.*;
//import java.IOException;
import java.util.*;
public class Binary
{
static void decToBinary(int n)
{
int[] binaryNum = new int[1000];
int i = 0;
while (n > 0)
{
binaryNum[i] = n % 2;
n = n / 2;
i++;
}
for (int j = i - 1; j >= 0; j--)
System.out.print("" +binaryNum[j]);
}
static void binaryToDecimal(int n)
{
String binaryString="1010";
int decimal=Integer.parseInt(binaryString,2);
System.out.println(decimal);
}
public static void main (String[] args)
{
// This code are converted the Decimal to Binary Number
System.out.println("Enter any Number");
Scanner sc=new Scanner(System.in);
int n=sc.nextInt();
decToBinary(n);
// end of the code
// this code are converted the Decimal to Hexadecimal Number
System.out.println();
String hexString = Integer.toHexString(n);
System.out.println("decimal to hexadecimal: " + hexString);
System.out.println("Enter a Negitev Integer Number");
Scanner cs=new Scanner(System.in);
int bin=cs.nextInt();
System.out.println("Converting integer "+bin+" to Hex Number: "+Integer.toHexString(bin));
// End of the Code
// this code are converted HexaDecimal to Decimal Number
Scanner scanner = new Scanner(System.in);
int num;
System.out.println("HexaDecimal to Decimal");
Scanner scan = new Scanner(System.in);
System.out.println("Enter the number :");
num = Integer.parseInt(scan.nextLine(),16);
String decimal = Integer.toString(num);
System.out.println("Decimal Value is : " + decimal);
Binary b=new Binary();
// end of the code
/* System.out.print("Enter any hexadecimal number: ");
String hexnum = scanner.nextLine();
int num = Integer.parseInt(hexnum,-0|16);
System.out.println(hexnum);
System.out.println("Decimal equivalent of given hex number: "+num);
*/
/* for(int i=0;i<=127;i++)
{
char ch = (char) i;
System.out.println(i + "= " + ch);
}
*/
}
}