S-JIS[2021-09-19] 変更履歴

HexFormat

JavaのHexFormatのメモ。


概要

HexFormatクラスは、整数やバイト配列と十六進数文字列との変換を行うユーティリティー。Java17で導入された。

import java.util.HexFormat;

	HexFormat hex = HexFormat.of();
	String s = hex.toHexDigits((short)100); // → "0064"
	int n = HexFormat.fromHexDigits("0064"); // → 100
	byte[] bytes = hex.parseHex("ff01"); // → [-1, 1]

	HexFormat hex = HexFormat.ofDelimiter(", ");
	String s = hex.formatHex(new byte[] {1,2,3,-1}); // → "01, 02, 03, ff"
	byte[] bytes = hex.parseHex("01, 00, ff"); // →[1, 0, -1]

HexFormatはスレッドセーフである。


HexFormatのインスタンス生成

HexFormatのインスタンスはof()で取得し、with系メソッドでパラメーターを変更したインスタンスを生成する。

HexFormatは以下のパラメーターを持っている。

パラメーター デフォルト値 設定 確認 説明
delimiter 空文字列 withDelimiter(s) delimiter() バイト配列を文字列化するときの区切り文字
prefix 空文字列 withPrefix(s) prefix() バイト配列を文字列化するときの接頭辞
suffix 空文字列 withSuffix(s) suffix() バイト配列を文字列化するときの接尾辞
upperCase/loweCase lowerCase withUpperCase()
withLowerCase()
isUpperCase() 十六進数のアルファベットの大文字/小文字

HexFormatのインスタンス生成の例

インスタンス生成の例 使用例 結果 感想
hex = HexFormat.of(); hex.formatHex(new byte[]{ 1,2,-1 }) 0102ff  
hex = HexFormat.of().withUpperCase(); 0102FF  
hex = HexFormat.ofDelimiter(", ");
hex = HexFormat.of().withDelimiter(", ");
01, 02, ff  
hex = HexFormat.ofDelimiter(", ")
.withPrefix("[").withSuffix("]");
[01], [02], [ff] 全体じゃなくて各バイトを囲むのかよ!w
hex = HexFormat.ofDelimiter(",")
.withPrefix("\"").withSuffix("\"");
"01","02","ff"  

なお、HexFormat.of()で返るインスタンスは常に同じ(シングルトンパターン)。
インスタンス内のパラメーターを変更することは出来ないのでイミュータブルであり、スレッドセーフ。


HexFormatのメソッド

import java.util.HexFormat;

	HexFormat hex  = HexFormat.of();
	HexFormat hex2 = HexFormat.ofDelimiter(", ").withPrefix("[").withSuffix("]");
メソッド 戻り型 引数 結果 説明
formatHex String byte[] bytes hex.formatHex(new byte[]{ 1,-1 })
hex2.formatHex(new byte[]{ 1,-1 })
"01ff"
"[01], [ff]"
バイト配列をひとつの文字列に変換する。
delimiterが指定されると、各バイトをその文字で区切る。
prefixやsuffixが指定されると、各バイトがその文字で囲まれる。
String byte[] bytes
int fromIndex
int toIndex
   
String Appendable out
byte[] bytes
var sb = new StringBuilder();
hex.formatHex(sb, new byte[]{ 1,-1 });
"01ff"
String Appendable out
byte[] bytes
int fromIndex
int toIndex
   
parseHex byte[] CharSequence s hex.parseHex("0102ff")
hex2.parseHex("0102ff")
hex2.parseHex("[01], [02], [ff]")
1, 2, -1
例外発生
1, 2, -1
文字列をバイト配列に変換する。
文字列の書式は、formatHexで出力されるのと同じでないといけない。
つまり、delimiterやprefix・suffixが指定されているときは、それと全く同じである必要がある。
合っていないとIllegalArgumentExceptionが発生する。
(delimiterのカンマの後のスペースの有無まで完全一致してないと駄目)
byte[] CharSequence s
int fromIndex
int toIndex
   
toLowHexDigit char int value hex.toLowHexDigit(0xef) 'f' バイトの下4ビットの十六進文字を返す。
toHighHexDigit char int value hex.toHighHexDigit(0xef) 'e' バイトの上4ビットの十六進文字を返す。
toHexDigits String byte value hex.toHexDigits((byte)-1) "ff" プリミティブの値を十六進数文字列に変換する。
String Appendable out
byte value
var sb = new StringBuilder();
hex.toHexDigits(sb, (byte)-1);
ff
String char value hex.toHexDigits('A') "0041"
String short value hex.toHexDigits((short)256) "0100"
String int value hex.toHexDigits(256) "00000100"
String long value hex.toHexDigits(256L) "0000000000000100"
String long value
int digits
hex.toHexDigits(258L, 6)
hex.toHexDigits(258L, 2)
"000102"
"02"
第2引数で指定された桁だけ出力する。
最大16桁。

staticメソッド

メソッド 戻り型 引数 結果 説明
isHexDigit boolean int ch HexFormat.isHexDigit('0')
HexFormat.isHexDigit('a')
HexFormat.isHexDigit('A')
HexFormat.isHexDigit('G')
true
true
true
false
十六進数の文字かどうかを返す。
fromHexDigit int int ch HexFormat.fromHexDigit('a') 10 十六進数の文字が表す値を返す。
十六進数の文字でない場合はNumberFormatExceptionが発生する。
fromHexDigits int CharSequence s HexFormat.fromHexDigits("0102") 258 十六進数文字列をintに変換する。
Integer.parseInt(s, 16)やparseUnsignedIntとほぼ同等だが、
Integerは全角文字でも受け付けるのに対し
HexFormatはASCIIの文字しか対応していない。
また、Integer.parseIntは正負符号も処理するが、
HexFormat.fromHexDigitsは対応していない。
int CharSequence s
int fromIndex
int toIndex
   
fromHexDigitsToLong long CharSequence s HexFormat.fromHexDigitsToLong("0102") 258 十六進数文字列をlongに変換する。
Long.parseLong(s, 16)やparseUnsignedLongとの関係は、fromHexDigitsと同様。
long CharSequence s
int fromIndex
int toIndex
   

Java目次へ戻る / 新機能へ戻る / 技術メモへ戻る
メールの送信先:ひしだま