ligo/gitlab-pages/docs/reference/bytes.md
2020-06-11 17:49:48 +02:00

4.4 KiB

id title description hide_table_of_contents
bytes-reference Bytes Operations on bytes true

import Syntax from '@theme/Syntax'; import SyntaxTitle from '@theme/SyntaxTitle';

function concat : bytes -> bytes -> bytes val concat : bytes -> bytes -> bytes let concat: (bytes, bytes) => bytes

Concatenate together two bytes arguments and return the result.

function concat_op (const s : bytes) : bytes is Bytes.concat(s , 0x7070)

Note that bytes_concat is deprecated.

let concat_op (s : bytes) : bytes =
   Bytes.concat s 0x7070
let concat_op = (s: bytes): bytes => Bytes.concat(s, 0x7070);
function sub : nat -> nat -> bytes -> bytes val sub : nat -> nat -> bytes -> bytes let sub : (nat, nat, bytes) => bytes

Extract bytes from start to length. For example if you gave the input "ff7a7aff" to the following function:

function slice_op (const s : bytes) : bytes is Bytes.sub(1n , 2n , s)

Note that bytes_slice is deprecated.

let slice_op (s : bytes) : bytes = Bytes.sub 1n 2n s

Note that Bytes.slice is deprecated.

let slice_op = (s: bytes): bytes => Bytes.sub(1n, 2n, s);

Note that Bytes.slice is deprecated.

It would return "7a7a".

function pack : 'a -> bytes val pack : 'a -> bytes let pack : 'a => bytes

Converts Michelson data structures to a binary format for serialization.

⚠️ PACK and UNPACK are features of Michelson that are intended to be used by people that really know what they're doing. There are several failure cases (such as UNPACKing a lambda from an untrusted source), most of which are beyond the scope of this document. Don't use these functions without doing your homework first.

function id_string (const p : string) : option(string) is block {
  const packed : bytes = bytes_pack(p) ;
} with (bytes_unpack(packed): option(string))
let id_string (p: string) : string option =
  let packed: bytes = Bytes.pack p in
  ((Bytes.unpack packed): string option)
let id_string = (p: string) : option(string) => {
  let packed : bytes = Bytes.pack(p);
  ((Bytes.unpack(packed)): option(string));
};
function unpack : bytes -> option 'a val unpack : bytes -> 'a option let unpack: bytes => option('a)

Reverses the result of using pack on data.

As the conversion might fail an option type is returned.

⚠️ PACK and UNPACK are features of Michelson that are intended to be used by people that really know what they're doing. There are several failure cases (such as UNPACKing a lambda from an untrusted source), most of which are beyond the scope of this document. Don't use these functions without doing your homework first.

function id_string (const p : string) : option(string) is block {
  const packed : bytes = bytes_pack(p) ;
} with (bytes_unpack(packed): option(string))
let id_string (p: string) : string option =
  let packed: bytes = Bytes.pack p in
  ((Bytes.unpack packed): string option)
let id_string = (p: string) : option(string) => {
  let packed : bytes = Bytes.pack(p);
  ((Bytes.unpack(packed)): option(string));
};
function length : bytes -> nat val length : bytes -> nat let length: bytes => nat