import
java.util.HashMap;
public
class
GFG {
public
static
String decToHex(
int
num) {
HashMap<Integer, Character> hexLookupTable =
new
HashMap<>();
hexLookupTable.put(
0
,
'0'
);
hexLookupTable.put(
1
,
'1'
);
hexLookupTable.put(
2
,
'2'
);
hexLookupTable.put(
3
,
'3'
);
hexLookupTable.put(
4
,
'4'
);
hexLookupTable.put(
5
,
'5'
);
hexLookupTable.put(
6
,
'6'
);
hexLookupTable.put(
7
,
'7'
);
hexLookupTable.put(
8
,
'8'
);
hexLookupTable.put(
9
,
'9'
);
hexLookupTable.put(
10
,
'A'
);
hexLookupTable.put(
11
,
'B'
);
hexLookupTable.put(
12
,
'C'
);
hexLookupTable.put(
13
,
'D'
);
hexLookupTable.put(
14
,
'E'
);
hexLookupTable.put(
15
,
'F'
);
StringBuilder res =
new
StringBuilder();
if
(num ==
0
) {
return
"0"
;
}
if
(num >
0
) {
while
(num >
0
) {
res.insert(
0
, hexLookupTable.get(num %
16
));
num /=
16
;
}
}
else
{
long
n = (
long
) num & 0xffffffffL;
while
(n >
0
) {
res.insert(
0
, hexLookupTable.get((
int
) (n %
16
)));
n /=
16
;
}
}
return
res.toString();
}
public
static
void
main(String[] args) {
int
x =
134
, y = -
1
, z = -
234
;
System.out.println(
"Hexadecimal representation for:"
);
System.out.println(x +
" is "
+ decToHex(x));
System.out.println(y +
" is "
+ decToHex(y));
System.out.println(z +
" is "
+ decToHex(z));
}
}