FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
Java/Conversions/DecimalToHexaDecimal.java at master · java66liu/Java · GitHub
java66liu
/
Java
Public
forked from
TheAlgorithms/Java
Notifications
You must be signed in to change notification settings
Fork
0
Star
0
Code
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
Java
/
Conversions
/
DecimalToHexaDecimal.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
32 lines (29 loc) · 1.11 KB
Breadcrumbs
Java
/
Conversions
/
DecimalToHexaDecimal.java
Copy path
File metadata and controls
32 lines (29 loc) · 1.11 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
package
Conversions
;
//hex = [0 - 9] -> [A - F]
class
DecimalToHexaDecimal
{
private
static
final
int
sizeOfIntInHalfBytes
=
8
;
private
static
final
int
numberOfBitsInAHalfByte
=
4
;
private
static
final
int
halfByte
=
0x0F
;
private
static
final
char
[]
hexDigits
= {
'0'
,
'1'
,
'2'
,
'3'
,
'4'
,
'5'
,
'6'
,
'7'
,
'8'
,
'9'
,
'A'
,
'B'
,
'C'
,
'D'
,
'E'
,
'F'
};
// Returns the hex value of the dec entered in the parameter.
public
static
String
decToHex
(
int
dec
) {
StringBuilder
hexBuilder
=
new
StringBuilder
(
sizeOfIntInHalfBytes
);
hexBuilder
.
setLength
(
sizeOfIntInHalfBytes
);
for
(
int
i
=
sizeOfIntInHalfBytes
-
1
;
i
>=
0
; --
i
) {
int
j
=
dec
&
halfByte
;
hexBuilder
.
setCharAt
(
i
,
hexDigits
[
j
]);
dec
>>=
numberOfBitsInAHalfByte
;
}
return
hexBuilder
.
toString
().
toLowerCase
();
}
// Test above function.
public
static
void
main
(
String
[]
args
) {
System
.
out
.
println
(
"Test..."
);
int
dec
=
305445566
;
String
libraryDecToHex
=
Integer
.
toHexString
(
dec
);
String
decToHex
=
decToHex
(
dec
);
System
.
out
.
println
(
"Result from the library : "
+
libraryDecToHex
);
System
.
out
.
println
(
"Result decToHex method : "
+
decToHex
);
}
}
Back
|
FazBrowse Home
|
New Git URL