10-23-2012 11:20 PM
I need to encode a bytes array without padding, my sample code below
// dataBytes = [30, 57, 43, 1, 115, 84, -86, 19, 11, -29, -31, 58, 15, 7, -118, -3]
String res = Base64OutputStream.encodeAsString(dataBytes, 0, dataBytes.length, true, true);
the return res's value is: HjkrAXNUqhML4_E6DwgSiQ== (24 characters)
I've google and found that the equal sign means padding, can not manual remove it. The problem is my server only accept 23-character-encoded-without-padding.
How can I handle this?
10-24-2012 03:55 AM
If I remove ==, the decode (on server) will be wrong. I can't control the server so adding back == is impossible.
10-27-2012 06:36 PM
Standard Base64 will always turn 3 bytes into 4, and will always pad the encoded bytes to make sure it has 3 bytes to encode. See here for an explanation of this:
You have 16 bytes to encode, so Base64 will encode 18 bytes, converting every 3 into 4, so will make 24 bytes. If you want to use Base64, then you have no choice, that is how it works.
So if you need to send 23 bytes, then you need another encoding method or a Base64 variant - see the wiki article for options.