aiken-lang/stdlib

Suggested function: bytearray.to_hex / bytearray.from_hex

Quantumplation opened this issue · 3 comments

When debugging, it'd be super helpful to be able to hex-encode a bytearray so I can pass it to trace; You might just say use cbor.diagnostic, but I prefer the hex view over cbor.diagnostic.

There may be other use cases, though not many come to mind.

(feel free to close if you feel this is out of scope, just dropping the suggestion here while it's on my mind)

KtorZ commented

Honestly. from_hex is going to be a pain in the a** on chain, especially without bitwise primitives. Though writing an encoder may be more reasonable. Actually, so "reasonable" that we already did it 😬!

stdlib/lib/aiken/cbor.ak

Lines 212 to 237 in a467c81

fn encode_base16(bytes: ByteArray, ix: Int, builder: ByteArray) -> ByteArray {
if ix < 0 {
builder
} else {
let byte = index_bytearray(bytes, ix)
let msb = byte / 16
let lsb = byte % 16
let builder =
cons_bytearray(
msb + if msb < 10 {
48
} else {
55
},
cons_bytearray(
lsb + if lsb < 10 {
48
} else {
55
},
builder,
),
)
encode_base16(bytes, ix - 1, builder)
}
}

So I guess it's mainly about moving that function to the bytearray module

rvcas commented

@KtorZ I have a little example for to_hex at least