Public
Edited
Apr 21, 2024
Insert cell
Insert cell
function unpackTwo(packed) {
// Remove the '0x' prefix if present
const hexString = packed.startsWith("0x") ? packed.slice(2) : packed;

// Convert hex string to Uint8Array
const bytes = new Uint8Array(
hexString.match(/.{1,2}/g).map((byte) => parseInt(byte, 16))
);

// Extract lengths
const aLength = bytes[0];
const bLength = bytes[1 + aLength];

// Extract byte arrays for each string
const aBytes = bytes.slice(1, 1 + aLength);
const bBytes = bytes.slice(2 + aLength, 2 + aLength + bLength);

// Convert byte arrays back to strings
const a = new TextDecoder().decode(aBytes);
const b = new TextDecoder().decode(bBytes);

return [a, b];
}
Insert cell
unpackTwo("0x12312367656e4c6179657220506f696e740370454c0000000000000000000000")
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more