[英]byte array to Hex (in int format)
我有以下函数将字节数组转换为整数格式的十六进制。
private static int byteArray2Int(final byte[] hash) {
Formatter formatter = new Formatter();
for (byte b : hash) {
formatter.format("%02x", b);
}
String str = formatter.toString();
int hex = Integer.parseInt(str, 16); //number format exception
return hex;
}
-
而且我正在错误以下。 我知道格式化程序的值已经是十六进制的,但是我想以整数格式存储。
请问我该怎么办?
Exception in thread "main" java.lang.NumberFormatException: For input string: "202e4724bb138c1c60470adb623ac932"
at java.lang.NumberFormatException.forInputString(NumberFormatException.java:48)
如下使用BigInteger,而不要尝试将其存储在int中,因为您的String太长而无法容纳在int范围内。
String hex = "202e4724bb138c1c60470adb623ac932";
BigInteger bi = new BigInteger(hex, 16);
System.out.println(bi);
“ 202e4724bb138c1c60470adb623ac932”太大,无法放入int或long中。 这将需要16个字节(如果我没记错的话)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.