Problem description
Titanium.Utils.base64encode generates a truncated blob when operating on Chinese characters.
Code sample:
var original = '一些随机的中国文字';
var reference = '5LiA5Lqb6ZqP5py655qE5Lit5Zu95paH5a2X';
var encoded = Titanium.Utils.base64encode(original).text;
console.log('encoded: ' + encoded);
console.log('reference: ' + reference);
console.log('encoded ' + (encoded === reference ? '===' : '!==') + ' reference');
Steps to reproduce
1. Create a new mobile project
2. Paste this to app.js:
3. Run and check the console.
Output:
{noformat}
encoded: 5LiA5Lqb6ZqP
reference: 5LiA5Lqb6ZqP5py655qE5Lit5Zu95paH5a2X
encoded !== reference
{noformat}
Expected output:
{noformat}
encoded: 5LiA5Lqb6ZqP5py655qE5Lit5Zu95paH5a2X
reference: 5LiA5Lqb6ZqP5py655qE5Lit5Zu95paH5a2X
encoded === reference
{noformat}
When "original" is Base64-encoded, the result should be equal to "reference". However, the blob returned by Titanium.Utils.base64encode is significantly shorter than expected. Titanium.Utils.base64encode apparently cannot properly handle special characters.
We also have the same problem when Chinese characters are present in the string data we want to base64 encode.
I am able to reproduce this issue with the following environment; iPhone 7 (10.2) Studio 4.9.0.201705302345 Ti SDK 6.1.1.v20170620103414 Appc NPM 4.2.9 Appc CLI 6.2.1 Ti CLI 5.0.13 Alloy 1.9.11 Arrow 2.0.0 Xcode 8.2 (8C38) Node v4.8.2 Java 1.8.0_131
Marking as duplicate of TIMOB-20395.
Fixed as part of TIMOB-20395.
Closing as duplicate.