2020-01-31 00:13:24 +04:00
---
id: bytes-reference
2020-03-17 19:38:41 +04:00
title: Bytes
description: Operations on bytes
hide_table_of_contents: true
2020-01-31 00:13:24 +04:00
---
2020-03-04 17:19:00 +04:00
import Syntax from '@theme/Syntax';
2020-03-17 19:38:41 +04:00
import SyntaxTitle from '@theme/SyntaxTitle';
< SyntaxTitle syntax = "pascaligo" >
type bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
type bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
type bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "pascaligo" >
function concat : bytes -> bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val concat : bytes -> bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let concat: (bytes, bytes) => bytes
< / SyntaxTitle >
2020-01-31 00:13:24 +04:00
Concatenate together two `bytes` arguments and return the result.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 00:13:24 +04:00
```pascaligo
2020-03-17 19:38:41 +04:00
function concat_op (const s : bytes) : bytes is Bytes.concat(s , 0x7070)
2020-01-31 00:13:24 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `bytes_concat` is *deprecated*.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 00:13:24 +04:00
```cameligo
let concat_op (s : bytes) : bytes =
Bytes.concat s 0x7070
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 00:13:24 +04:00
```reasonligo
let concat_op = (s: bytes): bytes => Bytes.concat(s, 0x7070);
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-01-31 00:13:24 +04:00
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function sub : nat -> nat -> bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val sub : nat -> nat -> bytes -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let sub : (nat, nat, bytes) => bytes
< / SyntaxTitle >
2020-01-31 00:13:24 +04:00
Extract the bytes between `pos1` and `pos2` . **Positions are zero indexed and
inclusive**. For example if you gave the input "ff7a7aff" to the following:
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 00:13:24 +04:00
```pascaligo
2020-03-17 19:38:41 +04:00
function slice_op (const s : bytes) : bytes is Bytes.sub(1n , 2n , s)
2020-01-31 00:13:24 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `bytes_slice` is *deprecated*.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 00:13:24 +04:00
```cameligo
2020-03-17 19:38:41 +04:00
let slice_op (s : bytes) : bytes = Bytes.sub 1n 2n s
2020-01-31 00:13:24 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `Bytes.slice` is *deprecated*.
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 00:13:24 +04:00
```
2020-03-17 19:38:41 +04:00
let slice_op = (s: bytes): bytes => Bytes.sub(1n, 2n, s);
2020-01-31 00:13:24 +04:00
```
2020-03-17 19:38:41 +04:00
> Note that `Bytes.slice` is *deprecated*.
2020-03-04 17:19:00 +04:00
2020-03-17 19:38:41 +04:00
< / Syntax >
2020-01-31 00:13:24 +04:00
It would return "7a7a" rather than "ff7a" or "ff" or "7a".
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function pack : 'a -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val pack : 'a -> bytes
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let pack : 'a => bytes
< / SyntaxTitle >
2020-01-31 00:13:24 +04:00
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 `UNPACK`ing 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.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 00:13:24 +04:00
```pascaligo
function id_string (const p : string) : option(string) is block {
const packed : bytes = bytes_pack(p) ;
} with (bytes_unpack(packed): option(string))
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 00:13:24 +04:00
```cameligo
let id_string (p: string) : string option =
let packed: bytes = Bytes.pack p in
((Bytes.unpack packed): string option)
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 00:13:24 +04:00
```reasonligo
let id_string = (p: string) : option(string) => {
let packed : bytes = Bytes.pack(p);
((Bytes.unpack(packed)): option(string));
};
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-01-31 00:13:24 +04:00
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function unpack : bytes -> option 'a
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val unpack : bytes -> 'a option
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let unpack: bytes => option('a)
< / SyntaxTitle >
Reverses the result of using `pack` on data.
2020-01-31 00:13:24 +04:00
2020-03-17 19:38:41 +04:00
As the conversion might fail an option type is returned.
2020-01-31 00:13:24 +04:00
> ⚠️ `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 `UNPACK`ing 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.
2020-03-04 17:19:00 +04:00
< Syntax syntax = "pascaligo" >
2020-01-31 00:13:24 +04:00
```pascaligo
function id_string (const p : string) : option(string) is block {
const packed : bytes = bytes_pack(p) ;
} with (bytes_unpack(packed): option(string))
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "cameligo" >
2020-01-31 00:13:24 +04:00
```cameligo
let id_string (p: string) : string option =
let packed: bytes = Bytes.pack p in
((Bytes.unpack packed): string option)
```
2020-03-04 17:19:00 +04:00
< / Syntax >
< Syntax syntax = "reasonligo" >
2020-01-31 00:13:24 +04:00
```reasonligo
let id_string = (p: string) : option(string) => {
let packed : bytes = Bytes.pack(p);
((Bytes.unpack(packed)): option(string));
};
```
2020-03-04 17:19:00 +04:00
< / Syntax >
2020-03-17 19:38:41 +04:00
< SyntaxTitle syntax = "pascaligo" >
function length : bytes -> nat
< / SyntaxTitle >
< SyntaxTitle syntax = "cameligo" >
val length : bytes -> nat
< / SyntaxTitle >
< SyntaxTitle syntax = "reasonligo" >
let length: bytes => nat
< / SyntaxTitle >