ethan hur ethan hur - 1 year ago 174
Javascript Question

javascript GUID base64 conversion doesn't seem to work

I try to convert GUID in base64 string for some reasons in Node.js

My function is

function encode(guid) {
let buffer = new Buffer(guid.replace(/-/g, ""), 'hex');
let ret = buffer.toString('base64');
ret = ret.replace(/\//g, "_").replace(/\+/g, "-");
return ret.substring(0,22); //FIXME

function decode(encoded_string) {
let buffer = new Buffer(encoded_string + '==', 'base64');
let ret = buffer.toString('hex');
ret = ret.replace(/_/g, "/").replace(/-/g, "+");
return ret;

I tested with guid = 'c9a646d3-9c61-4cb7-bfcd-ee2522c8f633'

and the result was 'yaZG05xhTLe_ze4lIsj2Mw'

and also it has decoded well (without dash).

However, according to the

The converted result was '00amyWGct0y/ze4lIsj2Mw=='.

I believe I might did something wrong, but can't find what I did wrong.

Answer Source

Javascript convert GUID in string format into Base64

You can check that your conversion is same as the result here when it it Big Endian, so I don't think it is a big problem.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download