How can I encode base64 data (and not a string) in Javascript?

I am transferring functionality from the Objective-C iPhone app to the Javascript iPhone app (Appcelerator Titanium). In Objective-C, I have an NSData object that represents this token:

//NSData object printed to the console:
<0cd9f571 b0e66e6d ca410d12 f67a404a 7e64b9b5 d2483fd9 63a9267b 1c7609e2>

This is not a string, this is an NSData object - an object-oriented wrapper for a byte buffer. When I base64 encode an object, I get this result:

//base64 encoded NSData object
DNn1cbDmbm3KQQ0S9npASn5kubXSSD/ZY6kmexx2CeI=

In my javascript implementation, I have a string representation of the same token. It looks like this:

//string version of the token in my javascript implementation
0cd9f571b0e66e6dca410d12f67a404a7e64b9b5d2483fd963a9267b1c7609e2

When I base64 encode a string object in javascript, I get this result:

//base64 encoded token (string) in javascript
MGNkOWY1NzFiMGU2NmU2ZGNhNDEwZDEyZjY3YTQwNGE3ZTY0YjliNWQyNDgzZmQ5NjNhOTI2N2IxYzc2MDllMg==

The problem is that the web service I am sending does not want base64 encoding, it wants to get base64 encoded data! How can I do this in javascript?

+5
3

base64, . JS:

if (! Array.prototype.map) {
    Array.prototype.map = function(f) {
        var result = [];
        for (var i=0; i < this.length; ++i) {
            result[i] = f(this[i], i);
        }
        return result;
    }
}
String.prototype.b16decode = function() {
    return this.match(/../g).map(
        function (x) {
            return String.fromCharCode(parseInt(x, 16));
        }
    ).join('');
}

,

btoa('0cd9f571b0e66e6dca410d12f67a404a7e64b9b5d2483fd963a9267b1c7609e2'.b16decode())

( btoa - base64, )

"DNn1cbDmbm3KQQ0S9npASn5kubXSSD/ZY6kmexx2CeI="
+3
0

search for toDataUrl () function

0
source

All Articles