Converting BigInteger to a shorter string in Java

I am looking for a way to convert BigInteger to a very short string (as short as possible). The conversion must be reversible. In this case, conversion security is not a big deal. Anyone have recommendations or examples of how they will solve this problem?

+4
source share
2 answers

One easy way is to use BigInteger.toString(Character.MAX_RADIX) . For reverse use, use the following constructor: BigInteger(String val, int radix) .

+6
source

You can use Base64 encoding. Note that this example uses Apache commons-codec:

 BigInteger number = new BigInteger("4143222334431546643677890898767548679452"); System.out.println(number); String encoded = new String(Base64.encodeBase64(number.toByteArray())); System.out.println(encoded); BigInteger decoded = new BigInteger(Base64.decodeBase64(encoded)); System.out.println(decoded); 

prints:

 4143222334431546643677890898767548679452 DC0DmJRYaAn2AVdEZMvmhRw= 4143222334431546643677890898767548679452 
+8
source

All Articles