Search code examples
javaarraysstringasciistring-conversion

String to Byte Conversion and Back Again Not Returning Same Result (ASCII)


I'm having a few issues converting a string back to the appropriate value after it has been converted to bytes.

The initial string:

"0000000000Y        Yã"

Where the 'ã' is just a character value.

The conversion code:

byte[] b = s.getBytes(StandardCharsets.US_ASCII);

However when using to convert it back:

String str = new String(b, StandardCharsets.US_ASCII);

I recieve:

"0000000000Y        Y?"

Anyone know why this is?

Thanks.


Solution

  • ã is not an ASCII character, so how it is handled is given by the implementation

    https://docs.oracle.com/javase/8/docs/api/java/lang/String.html#getBytes-java.nio.charset.Charset-

    This method always replaces malformed-input and unmappable-character sequences with this charset's default replacement byte array.

    For this charset it comes out as '?'