If you are using Internet Explorer, please remove blackberry.com from your compatibility view settings.

Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
hoang
Posts: 5
Registered: ‎03-07-2012
My Device: developer
My Carrier: mobifone

How to remove padding while encoding with Base64OutputStream?

Hi all,

 

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?

 

Thank you,

Hoang

Please use plain text.
Developer
pankajace12
Posts: 192
Registered: ‎04-30-2011
My Device: Software
My Carrier: Airtel

Re: How to remove padding while encoding with Base64OutputStream?

Hi,

Why don't you remove ==.

Thanks
Please use plain text.
New Contributor
hoang
Posts: 5
Registered: ‎03-07-2012
My Device: developer
My Carrier: mobifone

Re: How to remove padding while encoding with Base64OutputStream?

Hi pankajace12,

 

If I remove ==, the decode (on server) will be wrong. I can't control the server so adding back == is impossible.

 

Any ideas?

 

Thank you,

Hoang

Please use plain text.
Developer
peter_strange
Posts: 19,601
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to remove padding while encoding with Base64OutputStream?

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:

http://en.wikipedia.org/wiki/Base64

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. 

Please use plain text.