【发布时间】:2018-03-05 09:45:06
【问题描述】:
我正在学习如何使用 java 的 Formatter 类。 我想将正字节转换为十六进制数,然后将其解析为两位数的字符串。
我的方法看起来像这样:
String toHexString(byte byteToConvert) {
StringBuilder hexBuilder = new StringBuilder();
hexBuilder.append(Integer.toHexString(byteToConvert));
return hexBuilder.toString();
}
有没有办法能够格式化字符串(或 StringBuilder)以获得两位数?
System.out.println(toHexString(127)); // Would like it to output "7f"
System.out.println(toHexString(1)); // Would like it to output "01"
【问题讨论】:
-
StringBuilder不是格式化程序 -
快速谷歌搜索显示this example 和this example 作为可能的选项
-
你想在哪里使用 Formatter 类?