0

Say i have a byte[] containing Base64 text.

I want to take this text end create a String out of it:

 str = new String(text,"charset");

when i create the new String how can i be sure that i am not ruing indormation from the original text.

i mean how can i be sure i am passing the right charset when doing new String(text, "charset").

In case the original encoding is "cp-1255" and i am doing str = new String(text,"UTF-8") might i be ruining the text?

Michael A
  • 5,770
  • 16
  • 75
  • 127
  • 1
    As Base64 is basically just ASCII encoding for any binary data, you are lost without any extra information. Also, we can't provide a helpful answer not knowing how your byte arrays are generated and where they come from. – jlordo Feb 05 '13 at 08:42
  • Use `new String(base64, StandardCharsets.US_ASCII);` – McDowell Feb 05 '13 at 09:11

3 Answers3

1

First you need to decrypt/decode the bytes using

BASE64Decoder decoder = new BASE64Decoder();
byte[] decodedBytes = decoder.decodeBuffer(encodedBytes);

Then you need to Discover character Encoding from Bytes

Community
  • 1
  • 1
TheWhiteRabbit
  • 15,480
  • 4
  • 33
  • 57
1

I think, you need to check Encoding conversion in java

Not to ruin original text, you need to read input in correct encoding as well.

Community
  • 1
  • 1
Igor K
  • 470
  • 6
  • 10
1

You need to know the character encoding, that depends on the origins of the Base64. Also, you must use the correct encoding, if the Base64 doesn't represent binary data in UTF-8, then using UTF-8 will just return garbage or throw an exception.

In this case, you need to do:

str = new String(bytes, "Windows-1255");

Where bytes is the byte array resulting from decoding a base64 string.

Esailija
  • 138,174
  • 23
  • 272
  • 326