Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: trying to update the standard for SE 8, so I don't want to call out specific solutions as "Java 8". It is OK to say when features were introduced.

...

Code Block
bgColor#ccccff
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
bgColor#ccccff
languagejava
BigInteger x = new BigInteger("530500452766");
byte[] byteArray = x.toByteArray();
String s = Base64.getEncoder().encodeToString( byteArray);
byteArray = Base64.getDecoder().decode(s);
x = new BigInteger(byteArray);

...