Encode hex to base32

Hi, does anyone know the easiest way to encode hex string (generated by k6 UUIDV4()) to ‘base32’?

Hi @justin-thomas-cko-k6,
the current k6 versions don’t include a builtin function for this feature. You need to research and import an external JavaScript module that contains this ability for doing it.

I hope it clarify.

1 Like

Hi @codebien,

Thanks for your reply.

Ya, as you said, I couldn’t find any builtin function within k6 but since importing external modules within k6 script is also not that straight forward, it makes it bit difficult.

Since I can’t directly import a base32 npm package (like base32) , looks like I would need to import the whole base32 code within my project and then use it to perform encoding.

Is that the only possible way right now with k6?

@justin-thomas-cko-k6

You can import directly the script from github as in

import base32 from "https://raw.githubusercontent.com/agnoster/base32-js/master/dist/base32.js";

var encoded = base32.encode('some data to encode')
var decoded = base32.decode(encoded)
console.log(encoded, decoded)
1 Like

@mstoykov

Thanks for a quicker solution.

Do you know if using this script we can control the output to be od 26 bytes rather than 52 bytes.

I am not getting your question.

If you do base32 encoding it is encoding in a particular way I am not certain you can control how big the output is, but maybe there are some variants I am not familiar.