How to base64 encode a string in NativeScript


#1

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:

  1. base-64
  2. utf8

Add these to your NativeScript project:

$ npm install base-64 --save
$ npm install utf8 --save

Once added, the rest is pretty easy. In a JavaScript (or TypeScript) file where you want to do the work:

JavaScript

var base64 = require('base-64');
var utf8 = require('utf8');

TypeScript

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 atob() and btoa() encodings, so if you need to interop with strings encoded by web browsers, it should work.

Hope this helps.


#2

Native methods

Encode

Android
const text = new java.lang.String(“Yolo 10000”);
const data = text.getBytes(“UTF-8”);
const base64 = android.util.Base64.encodeToString(data, android.util.Base64.DEFAULT);

IOS
const text = NSString.stringWithString(“Yolo 10000”);
const data = text.dataUsingEncoding(NSUTF8StringEncoding);
const base64 = data.base64EncodedStringWithOptions(0);

Decode

Android
const text = new java.lang.String(“Yolo 10000”);
const data = text.getBytes(“UTF-8”);
const base64 = android.util.Base64.encodeToString(data,android.util.Base64.DEFAULT);
android.util.Base64.decode(text, android.util.Base64.DEFAULT);

IOS
const decodedData = NSData.alloc().initWithBase64EncodedStringOptions(base64String,0);
const decodedString = NSString.alloc().initWithDataEncoding(decodedData,NSUTF8StringEncoding);


Show pdf in view
#3

How do you decode it back to string from a base64 string?


#4

Hello @mikevg08 @triniwiz

I need the same thing. I need to decode base64 string so I can write to file on iOS.
My actual problem is described in this thread Decode base64 string to PDF


#5

I did use base64 for an image to upload to a server, but converting to b64 an image from the device freeze the app while it was converting a medium size image… so I wont recomended…
I ended using http module to upload.


#6

For anyone that stumbles upon this, here’s a TypeScript file you can add to your project that exports an encoding function based on @triniwiz’s snippets. I called mine string-util.ts.

import { isIOS } from "tns-core-modules/platform";

declare var NSString: any;
declare var NSUTF8StringEncoding: any;
declare var java: any;
declare var android: any;

export function base64Encode(value) {
  if (isIOS) {
    let text = NSString.stringWithString(value);
    let data = text.dataUsingEncoding(NSUTF8StringEncoding);
    return data.base64EncodedStringWithOptions(0);
  } else {
    let text = new java.lang.String(value);
    let data = text.getBytes("UTF-8");
    return android.util.Base64.encodeToString(data, android.util.Base64.DEFAULT);
  }
}

#7

is it to convert to base64 a video file?