如何在java中将字节数组转换为BigInteger
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4182029/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to convert byte array to BigInteger in java
提问by saggy
I'm working on java... I want to know how to convert array of byte into BigInteger. Actually i used md5's digest method which returned me array of byte which i want to convert into Biginteger.
我正在研究 java... 我想知道如何将字节数组转换为 BigInteger。实际上,我使用了 md5 的摘要方法,该方法返回了我想转换为 Biginteger 的字节数组。
回答by aioobe
This example Get MD5 hash in a few lines of Javahas a related example.
这个例子Get MD5 hash in几行Java有一个相关的例子。
I believe you should be able to do
我相信你应该能够做到
MessageDigest m=MessageDigest.getInstance("MD5");
m.update(message.getBytes(), 0, message.length());
BigInteger bi = new BigInteger(1,m.digest());
and if you want it printed in the style "d41d8cd98f00b204e9800998ecf8427e"
you should be able to do
如果你希望它以"d41d8cd98f00b204e9800998ecf8427e"
你应该能够做的样式打印
System.out.println(bi.toString(16));
回答by Stephen C
Actually i used md5's digest method which returned me array of byte which i want to convert into a
BigInteger
.
实际上,我使用了 md5 的摘要方法,该方法返回了我想转换为
BigInteger
.
You can use new BigInteger(byte[])
.
您可以使用new BigInteger(byte[])
.
However, it should be noted that the MD5 hash is not really an integer in any useful sense. It is really just a binary bit pattern.
但是,应该注意的是,MD5 散列在任何有用的意义上都不是真正的整数。它实际上只是一个二进制位模式。
I guess you are just doing this so that you can print or order the MD5 hashes. But there are less memory hungry ways of accomplishing both tasks.
我猜您这样做只是为了打印或订购 MD5 哈希值。但是完成这两项任务的内存占用较少的方法。