Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert byte array to BigInteger in java

Tags:

java

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.

like image 323
saggy Avatar asked Nov 15 '10 06:11

saggy


People also ask

How do I create an array of BigInteger in Java?

BigInteger class provides operations for modular arithmetic, GCD calculation, primality testing, prime generation, bit manipulation, and a few other miscellaneous operations. BigInteger bInteger = new BigInteger(arr); The following is an example that creates BigInteger from a byte array in Java.

How many bytes is BigInteger Java?

BigInteger: int bitCount +4 bytes int bitLength +4 bytes int firstNonzeroIntNum +4 bytes int lowestSetBit +4 bytes int signum +4 bytes int[] mag +? That's a total of 20 bytes + the integer array.

How many bytes is a BigInteger?

A big integer is a binary integer that occupies 8 bytes. The range of big integers is -9223372036854775808 to +9223372036854775807.

How do you convert bytes to long objects?

The BigInteger class has a longValue() method to convert a byte array to a long value: long value = new BigInteger(bytes). longValue();


2 Answers

This example Get MD5 hash in a few lines of Java has a related example.

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

System.out.println(bi.toString(16));
like image 122
aioobe Avatar answered Nov 08 '22 14:11

aioobe


Actually i used md5's digest method which returned me array of byte which i want to convert into a BigInteger.

You can use 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.

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.

like image 44
Stephen C Avatar answered Nov 08 '22 14:11

Stephen C