Reputation: 69
I am trying to convert some code of python to java. And now stuck with the python base64 encoder with values greater than 63.
python:
s = chr(59) + chr(234)
print(base64.b64encode(s))
>> O+o=
java
char[] chars = {59, 234};
String data = new String(chars);
byte[] encoded = Base64.encodeBase64(data.getBytes());
System.out.println(new String(encoded));
>> O8Oq
Anyone any idea?
Upvotes: 1
Views: 824
Reputation: 69
Here is the final java code:
char[] chars = {59, 234};
String data = new String(chars);
byte[] encoded = Base64.encodeBase64(data.getBytes("ISO-8859-1"));
System.out.println(new String(encoded));
>> O+o=
Thank you all for help. :)
Upvotes: 0
Reputation: 111
Note that your snippet in Python already deals with bytes whereas the Java version starts with Unicode characters and then transforms them to bytes by calling data.getBytes()
.
You can achieve the same result in Python by encoding to UTF-8 first:
>>> (unichr(59) + unichr(234)).encode('utf-8').encode('base64')
'O8Oq\n'
Upvotes: 1