...
| Code Block | ||
|---|---|---|
| ||
BigInteger x = new BigInteger("530500452766");
byte // convert x to a String
byte[] byteArray = x.toByteArray(); // convert to byte array
String s = new String(byteArray); // s prints as "{,J?z" -
// the fourth character is invalid
// convert s back to a BigInteger
byteArray = s.getBytes(); // convert to bytes
x = new BigInteger(byteArray); // now x = 530500435870
|
When this program was run on a Linux platform where the default character encoding is US-ASCII, the string s got the value {?J??, because some of the characters were unprintable. When converted back to a BigInteger, x got the value 149830058370101340468658109.
...
| Code Block | ||
|---|---|---|
| ||
BigInteger x = new BigInteger("530500452766");
String s = x.toString(); // valid character data
try {
byte[] byteArray = s.getBytes("UTF8");
// ns prints as "530500452766"
String ns = new String(byteArray, "UTF8");
// construct the original BigInteger
BigInteger x1 = new BigInteger(ns);
} catch (UnsupportedEncodingException ex) {
// handle error
}
|
...
Attempting to read a byte array containing raw binary data as if it were character data may can produce erroneous results.
...
<ac:structured-macro ac:name="unmigrated-wiki-markup" ac:schema-version="1" ac:macro-id="4790cc3856f1d62c-da8c5f91-4f114a1f-ad128346-ce9373e656261964ff51313d"><ac:plain-text-body><![CDATA[ | [[API 2006 | AA. Bibliography#API 06]] | [Class String | http://java.sun.com/javase/6/docs/api/java/lang/String.html] | ]]></ac:plain-text-body></ac:structured-macro> |
...