> For the complete documentation index, see [llms.txt](https://subsembly.gitbook.io/subsembly/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://subsembly.gitbook.io/subsembly/advanced/as-scale-codec.md).

# as-scale-codec

### Overview

**`as-scale-codec`** is an AssemblyScript implementation of Polkadot SCALE Codec. The codec is used as a communication mechanism between Substrate Hosts and Substrate Runtimes.&#x20;

This is an integral part of the **`Subsembly`**, since it provides SCALE encoding and decoding functionality for the types.&#x20;

The library is maintained by LimeChain and has improved significantly over the course of **`Subsembly`** development.&#x20;

### Codec&#x20;

Every **`Subsembly`** type needs to have encoding and decoding features, which is provided from the **`Codec`** interface of **`as-scale-codec`**. If you were to create your custom type to use in your **`Subsembly`** runtime, type has to implement **`Codec`** interface.

### Examples

#### Encoding

Every type has а **toU8a** function. It encodes type value into an array of bytes

```
import { Bool, String } from "as-scale-codec"

// Bool
const scaleBool = new Bool(true);
scaleBool.toU8a() // => [0x01]

// String
const scaleString = new ScaleString("a");
scaleString.toU8a() // => [0x04, 0x61] 

// UInt64
const uInt64 = new UInt64(10);
uInt64.toU8a(); // => [0x0A, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00]
```

#### Decoding

We can decode arbitrary bytes into as-scale-codec using BytesReader:

```
import { BytesReader } from 'as-scale-codec';

// Arbitrary SCALE encoded bytes
const bytes: u8[] = [
    0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
    69, 0, 0, 0
];

// Instantiate BytesReader instance with SCALE encoded bytes
const bytesReader = new BytesReader(bytes);

// Read Int64
bytesReader.readInto<Int64>();
// => new Int(-1)

// Read UInt32
bytesReader.readInto<UInt32>();
// => new UInt32(69)
```

For more information, please visit [as-scale-codec](https://github.com/LimeChain/as-scale-codec) and SCALE [docs](https://substrate.dev/docs/en/knowledgebase/advanced/codec).


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://subsembly.gitbook.io/subsembly/advanced/as-scale-codec.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
