Convert between hex strings and byte arrays with HexFormat.
Porównanie kodu
✕ Java 8
// Pad to 2 digits, uppercase
String hex = String.format(
"%02X", byteValue);
// Parse hex string
int val = Integer.parseInt(
"FF", 16);
✓ Java 17+
var hex = HexFormat.of()
.withUpperCase();
String s = hex.toHexDigits(
byteValue);
byte[] bytes =
hex.parseHex("48656C6C6F");
Widzisz problem z tym kodem? Daj nam znać.
Dlaczego nowoczesne podejście wygrywa
Bidirectional
Convert bytes→hex and hex→bytes with one API.
Configurable
Delimiters, prefix, suffix, upper/lower case.
Array support
Encode/decode entire byte arrays at once.
Stare podejście
Manual Hex Conversion
Nowoczesne podejście
HexFormat
Od JDK
17
Poziom trudności
Średniozaawansowany
Wsparcie JDK
HexFormat
Dostępne
Widely available since JDK 17 LTS (Sept 2021)
Jak to działa
HexFormat provides bidirectional hex encoding/decoding for bytes, ints, and arrays. Configure delimiters, prefix, suffix, and case. No more manual formatting or parsing.
Powiązana dokumentacja