문제

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[].

다른 팁

You cannot just blindly use any charset you want. Strings in Java and Javascript use UTF-16. Once you have decoded the base64 data into a byte array, you have to know the exact charset those bytes actually represent so they can be converted to UTF-16 correctly without losing any data. You have to know the charset that was used when the data was base64 encoded. If you do not know the exact charset, you are left with heuristic analysis or just plain guessing, and both are not reliable enough. Either both parties must agree on a common charset ahead of time, or else the charset needs to be exchanged along with the base64 data.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top