Java Charset that supports all symbols using 8 bits per symbol from [0-255] ranges per character

advertisements

I'm trying to pass a byte array with any kind of data ranging from 0 to 255 per element.

I have to pass it into Javascript so I convert it into a String, but some characters get lost and replaced with 0x3F Question Mark.

Whats the proper Charset that supports all 8 bit symbols to transfer to Javascript.

public String base64Decode(String s) {
  //... lots of stuff transforming String into byte array.

  //Some example bytes shown here.
  byte[] destArray = {(byte)0xf3, (byte)0xc3, 00, 01, 00, 00, 00, 00, (byte)0xc3, (byte)0x63, (byte)0x2d, 00, 00, 00, 00, 00, (byte)0xe0, (byte)0x9d, (byte)0xea};
  System.out.println(new String(destArray, Charset.forName("UTF-8")));
  return new String(new String(destArray, Charset.forName("UTF-8")));
}

I output the System.out.println into a file using a batch script

java Test > out.bin

Then compare byte by byte to see what is lost.
To sum it up 0x9D becomes 0x3D which is wrong.
There are probably others too but I didn't check the whole file its over 2 megs in size.

The default new String(destArray); does a better job but still misses a few characters.


You can use ISO-8859-1.

However, it's an ugly hack that should only be used if something really prevents you from using correct datatypes (i.e. using byte[] for binary data).

From the common sense, base64 is a way to represent binary data as ASCII strings, therefore base64Decode() should take a String and return a byte[].