diff --git a/lib/aiken/string.ak b/lib/aiken/string.ak index 093cb0d..11d7179 100644 --- a/lib/aiken/string.ak +++ b/lib/aiken/string.ak @@ -26,9 +26,15 @@ test concat_3() { /// Convert a `ByteArray` into a `String` /// +///
⚠️
WARNING
| This functions fails if the underlying `ByteArray` isn't UTF-8-encoded.
In particular, you cannot convert arbitrary hash digests using this function.
For converting arbitrary `ByteArray`s, use [bytearray.to_hex](../bytearray.html#to_hex). +/// --- | --- +/// /// ```aiken /// string.from_bytearray("foo") == @"foo" +/// /// string.from_bytearray(#"666f6f") == @"foo" +/// +/// string.from_bytearray(some_hash) -> fail /// ``` pub fn from_bytearray(bytes: ByteArray) -> String { decode_utf8(bytes) diff --git a/lib/scratchpad.ak b/lib/scratchpad.ak new file mode 100644 index 0000000..02b1ef7 --- /dev/null +++ b/lib/scratchpad.ak @@ -0,0 +1,24 @@ +use aiken/bytearray.{concat, push} +use aiken/cbor + +pub fn serialise_int(value: Int) -> ByteArray { + let bytes = + #"" + |> push(value) // >> 0 + |> push(value / 256) // >> 8 + |> push(value / 65536) // >> 16 + |> push(value / 16777216) + bytes +} + +pub fn serialise_long(value: Int) -> ByteArray { + serialise_int(value / 4294967296) |> concat(serialise_int(value)) +} + +test serialise_long_1() { + serialise_long(10737418240) == #"0000000280000000" +} + +test serialise_builtin() { + cbor.serialise(10737418240) == #"1b0000000280000000" +}