Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X
Is there a built in method for base64 conversion (both to and from?) If so, what is it? Or do I need to create my own from some of the many open source resources?
There are 'built in' Base64 encode/decode functions ... but they are not YET exposed in a resource library. I have requested that they be exposed in subsequent releases.
I could see the following functions in the Snippets tab: base64EncodeString, base64EncodeByte, base64DecodeString, base64DecodeByte.
However, I get the following error when I run service using these functions
ReferenceError: "base64EncodeString" is not defined.
Regards
Arunkumar D
The Base64 methods were available in older versions of Thingworx (I think before version 5). In version 5 the engineers didn't expose it, but the references in the Snippets were still there. I think the Base64 methods are made available again in maybe version 6.0.4, but I haven't looked.
I have a Service I use, instead of using the built-in functions. It's invoked like this:
var b64_string = Things.Convert.toBase64({ from : "some string" });
//
// Convert.toBase64.js
//
// [Conversion Service]
//
// OUTPUT
// STRING
//
// INPUT
// * from : STRING
//
// jshint strict:false
// jshint bitwise:false
/* global from */
/* exported result */
var Base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
encode: function(e) {
var result = "";
var byte1, byte2, byte3, char1, char2, char3, char4;
var f = 0;
e = Base64._utf8_encode(e);
while (f < e.length) {
byte1 = e.charCodeAt(f++);
byte2 = e.charCodeAt(f++);
byte3 = e.charCodeAt(f++);
char1 = byte1 >> 2;
char2 = (byte1 & 3) << 4 | byte2 >> 4;
char3 = (byte2 & 15) << 2 | byte3 >> 6;
char4 = byte3 & 63;
if (isNaN(byte2)) {
char3 = char4 = 64;
} else if (isNaN(byte3)) {
char4 = 64;
}
result = result + this._keyStr.charAt(char1) + this._keyStr.charAt(char2) + this._keyStr.charAt(char3) + this._keyStr.charAt(char4);
}
return result;
},
_utf8_encode: function(string) {
string = string.replace(/\r\n/g, "\n");
var result = "";
for (var index = 0; index < string.length; index++) {
var char = string.charCodeAt(index);
if (char < 128) {
result += String.fromCharCode(char);
} else if (char > 127 && char < 2048) {
result += String.fromCharCode(char >> 6 | 192);
result += String.fromCharCode(char & 63 | 128);
} else {
result += String.fromCharCode(char >> 12 | 224);
result += String.fromCharCode(char >> 6 & 63 | 128);
result += String.fromCharCode(char & 63 | 128);
}
}
return result;
}
};
var result = Base64.encode(from);
And, the complementary Service, called like:
var some_string = Things.Convert.toString_fromBase64({ from : "c29tZSBzdHJpbmc=" });
//
// Convert.toString_fromBase64.js
//
// [Conversion Service]
//
// OUTPUT
// STRING
//
// INPUT
// * from : Base 64 encoded STRING
//
// jshint strict:false
// jshint bitwise:false
/* global from */
/* exported result */
var Base64 = {
_keyStr: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=",
decode: function(string) {
var result = "";
var byte1, byte2, byte3, char1, char2, char3, char4;
var f = 0;
string = string.replace(/[^A-Za-z0-9\+\/\=]/g, "");
while (f < string.length) {
char1 = this._keyStr.indexOf(string.charAt(f++));
char2 = this._keyStr.indexOf(string.charAt(f++));
char3 = this._keyStr.indexOf(string.charAt(f++));
char4 = this._keyStr.indexOf(string.charAt(f++));
byte1 = char1 << 2 | char2 >> 4;
byte2 = (char2 & 15) << 4 | char3 >> 2;
byte3 = (char3 & 3) << 6 | char4;
result = result + String.fromCharCode(byte1);
if (char3 !== 64) {
result = result + String.fromCharCode(byte2);
}
if (char4 !== 64) {
result = result + String.fromCharCode(byte3);
}
}
result = Base64._utf8_decode(result);
return result;
},
_utf8_decode: function(string) {
var result = "";
var index = 0;
var char1 = 0;
var char2 = 0;
var char3 = 0;
while (index < string.length) {
char1 = string.charCodeAt(index);
if (char1 < 128) {
result += String.fromCharCode(char1);
index++;
} else if (char1 > 191 && char1 < 224) {
char2 = string.charCodeAt(index + 1);
result += String.fromCharCode((char1 & 31) << 6 | char2 & 63);
index += 2;
} else {
char2 = string.charCodeAt(index + 1);
char3 = string.charCodeAt(index + 2);
result += String.fromCharCode((char1 & 15) << 12 | (char2 & 63) << 6 | char3 & 63);
index += 3;
}
}
return result;
}
};
var result = Base64.decode(from);