apps/docs/src/guide/encoding/working-with-bytes.md
This guide aims to give a high-level overview of how to work with bytes in the SDK and the structures we expect them to take. For a complete overview of ABI Encoding generally, within the Fuel network, we recommend you see the ABI Encoding documentation.
We know the sizes of all core types at compile time. They are the building blocks of the more complex types and are the most common types you will encounter.
u8 / u16 / u32 / u64 / u128 / u256)Each type will only contain the number of bits specified in the name. For example, a u8 will contain 8 bits, and a u256 will contain 256 bits and take up the exact property space with no additional padding.
<<< @./snippets/working-with-bytes.ts#working-with-bytes-1{ts:line-numbers}
A boolean is encoded as a single byte like a u8, its value being either 0 or 1.
<<< @./snippets/working-with-bytes.ts#working-with-bytes-2{ts:line-numbers}
A fixed-length string's size is known at compile time due to the argument declaration of str[n] with n denoting its length. Each character in the string is encoded as a utf-8 bit.
<<< @./snippets/working-with-bytes.ts#working-with-bytes-3{ts:line-numbers}
B256 / B512These are fixed-length byte arrays, with B256 containing 256 bits and B512 containing 512 bits. You can use them for address and signature formats.
<<< @./snippets/working-with-bytes.ts#working-with-bytes-4{ts:line-numbers}
These are the types that will contain nested types and no additional encoding is required other than the encoding of the nested types. This is relevant to arrays, tuples, and structs and enums. The only caveat here, is an enum will also contain a u64 representing the enum case value. options are encoded in the same way as enums.
<<< @./snippets/working-with-bytes.ts#working-with-bytes-5{ts:line-numbers}
Heap types are types with a dynamic length that we do not know at compile time. These are Vec, String, and raw_slice. These types are encoded with a u64 representing the length of the data, followed by the data itself.
<<< @./snippets/working-with-bytes.ts#working-with-bytes-6{ts:line-numbers}
Here is the full example of the working with bytes functions:
<<< @./snippets/working-with-bytes.ts#full{ts:line-numbers}