How to convert neuron ID to array of numbers?

When viewing a neuron on NNS dapp, the get_neuron request is sent to fetch the details of the neuron. But the request payload contains an array of number for the neuron ID instead of string ID:

How can I do thing conversion manually in JS?

These numbers are the same as the blob in your other forum post: Convert IDL blob to string

In Candid a vec nat8 is the same as a blob. Each number represents a value between 0 and 255, which is a byte within an array of bytes.

Looking at the NNS screenshot, the id looks like a hexadecimal string for these bytes.

In JS you could convert decimals to hex like this:

const hexString = yourByteNumberArray.map(byte => byte.toString(16).padStart(2, '0')).join('');

The other way around would be:

const byteNumberArray = hexString.match(/.{1,2}/g).map(hex => parseInt(hex, 16));
1 Like

You find these kind of utilities used by NNS dapp in @dfinity/utils (https://github.com/dfinity/ic-js/tree/main/packages/utils).

2 Likes