There are some old references on the Interwebs for completing this task, and none that show you how to do it with a string, so here’s a quick guide to base64 encoding strings in NativeScript.
The bulk of the solution depends on two npm packages:
Add these to your NativeScript project:
$ npm install base-64 --save $ npm install utf8 --save
var base64 = require('base-64'); var utf8 = require('utf8');
import * as base64 from "base-64"; import * as utf8 from "utf8";
Then in your code:
var str = "myfancystring"; var bytes = utf8.encode(str); var encodedStr = base64.encode(bytes); console.log(encodedStr);
That’s it! Easy since this npm plugins “just work” in NativeScript. The base64 library is also supposed to be compatible with the web’s
btoa() encodings, so if you need to interop with strings encoded by web browsers, it should work.
Hope this helps.