...
Code Block | ||
---|---|---|
| ||
BigInteger x = new BigInteger("530500452766"); String s = x.toString(); // valid character data byte[] byteArray = s.getBytes(); String ns = new String(byteArray); x = new BigInteger(ns); |
Compliant Solution (
...
Base64)
While Java does not provide a character set that guarantees lossless encoding of byte data, there are many other solutions for safely converting an arbitrary byte array into a string and back. Java 8 provides introduced the java.util.Base64
class which provides encoders and decoders for the Base64 encoding scheme. This compliant solution uses Base64 to safely convert the a number to a string and back without corruption of corrupting the data.
Code Block | ||||
---|---|---|---|---|
| ||||
BigInteger x = new BigInteger("530500452766");
byte[] byteArray = x.toByteArray();
String s = Base64.getEncoder().encodeToString( byteArray);
byteArray = Base64.getDecoder().decode(s);
x = new BigInteger(byteArray);
|
...