-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDec2Hex.java
More file actions
53 lines (42 loc) · 1.21 KB
/
Dec2Hex.java
File metadata and controls
53 lines (42 loc) · 1.21 KB
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
import java.util.Scanner;
class Dec2Hex
{
public static int Arg1;
public static void main(String args[])
{
if(args.length <= 0){
System.out.println("No decimal value given - exiting program...");
System.exit(0);
}
if (args.length > 0)
{
try
{
Arg1 = Integer.parseInt(args[0]);
Arg1 = Math.abs(Arg1);
}
catch (NumberFormatException e)
{
System.err.println("Argument" + args[0] + " must be an integer.");
System.exit(0);
}
}
if(Arg1==0){
System.out.println("Converting the Decimal Value " + Arg1 + " to Hex...");
System.out.println("Hexadecimal representation is : " + Arg1);
System.exit(0);
}
char ch[]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
int rem, num;
num = Arg1;
String hexadecimal="";
System.out.println("Converting the Decimal Value " + num + " to Hex...");
while(num != 0)
{
rem=num%16;
hexadecimal= ch[rem] + hexadecimal;
num= num/16;
}
System.out.println("Hexadecimal representation is : " + hexadecimal);
}
}