Waza_Be
Waza_Be

Reputation: 39558

Base64 encoding result in a lot of whitespaces (blank char that I cannot identify) at the end

I am using this simple code to encode "ab:ab" in Base64:

    try {
        byte[] data = "ab:ab".getBytes("UTF-8");
        String base64 = Base64.encodeToString(data, Base64.DEFAULT);
        Log.e("CVE","*"+base64+"*");
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }

But the result, that should be "YWI6YWI=" is in fact the same String with whitespaces at the end:

01-29 10:51:18.813  21789-21863/com.myapp.com E/CVE﹕
*YWI6YWJmZHNmZmRzZmRzc2RmZHM=
    *

Any idea what I am doing wrong???

Upvotes: 2

Views: 2309

Answers (1)

d311404
d311404

Reputation: 76

It could be an encoding issue because I get the good result if I remove the UTF-8 string in the call to getBytes:

byte[] data = "ab:ab".getBytes();

On Android, the default charset is UTF-8 and I do wonder why it's not working as expected in this example.

You may also want to use the NO_WRAP flag to get everything in one line or strip the resulting string:

base64 = Base64.encodeToString(data, Base64.NO_WRAP);

Upvotes: 6

Related Questions