【发布时间】:2018-09-15 19:56:06
【问题描述】:
代码sn-p是:
String str = "h";
StringBuffer buf = new StringBuffer();
byte[] bytes = str.getBytes("UTF-16BE");
for (int i = 0; i < bytes.length; i++) {
String byteAsHex = Integer.toHexString(bytes[i]);
buf.append(byteAsHex);
}
System.out.println(buf.toString());
输出为:068,其中拉丁小写字母 H 为 0068。
您能告诉我为什么缺少前导 0 吗?
【问题讨论】:
-
docs.oracle.com/javase/8/docs/api/java/lang/…:“这个值被转换成十六进制(以 16 为基数)的 ASCII 数字字符串没有额外的前导 0。”(强调我的)。 如果无符号幅度为零,则由单个零字符'0'表示