# Buffer

Operators and functions for working with an array of bytes (**buf** type) are described here.

* [bool( buf b ) bool](/stdlib/buffer.md#bool-buf-b-bool)
* [buf( str s ) buf](/stdlib/buffer.md#buf-str-s-buf)
* [str( buf b ) str](/stdlib/buffer.md#str-buf-b-str)
* [Base64( buf b ) str](/stdlib/buffer.md#base-64-buf-b-str)
* [DecodeInt( buf b, int offset ) int](/stdlib/buffer.md#decodeint-buf-b-int-offset-int)
* [Del( buf b, int off, int length ) buf](/stdlib/buffer.md#del-buf-b-int-off-int-length-buf)
* [EncodeInt( buf b, int i ) buf](/stdlib/buffer.md#encodeint-buf-b-int-i-buf)
* [Hex( buf b ) str](/stdlib/buffer.md#hex-buf-b-str)
* [Insert( buf b, int off, buf src) buf](/stdlib/buffer.md#insert-buf-b-int-off-buf-src-buf)
* [SetLen( buf b, int size ) buf](/stdlib/buffer.md#setlen-buf-b-int-size-buf)
* [Subbuf( buf b, int off, int length ) buf](/stdlib/buffer.md#subbuf-buf-b-int-off-int-length-buf)
* [UnBase64( str s ) buf](/stdlib/buffer.md#unbase-64-str-s-buf)
* [UnHex( str s ) buf](/stdlib/buffer.md#unhex-str-s-buf)
* [Write( buf b, int off, buf src ) buf](/stdlib/buffer.md#write-buf-b-int-off-buf-src-buf)

## Operators

| Operator             | Result | Description                                                                      |
| -------------------- | ------ | -------------------------------------------------------------------------------- |
| **\*** buf           | int    | Returns the number of bytes in the array.                                        |
| buf **?**            | bool   | Calls *bool(buf)*.                                                               |
| buf **+** buf        | buf    | Merges two buffers.                                                              |
| buf **=** buf        | buf    | Assignment operator.                                                             |
| buf **&=** buf       | buf    | Create a clone of the buffer. The new variable will work with the same data set. |
| buf **+=** buf       | buf    | Appends a buffer to a buffer variable.                                           |
| buf **+=** int       | buf    | Appends one byte to the buffer. The number must be less than 256.                |
| buf **+=** str       | buf    | Appends a string to the buffer.                                                  |
| buf **+=** char      | buf    | Appends a character to the buffer.                                               |
| buf **\[** int **]** | int    | Sets/gets a byte by index.                                                       |

## Functions

### bool(buf b) bool

The *bool* function returns *false* if the buffer is empty, otherwise, it returns *true*.

### buf(str s) buf

The *buf* function converts a string to a *buf* value and returns it.

### str(buf b) str

The *str* function converts a *buf* value to a string and returns it.

### Base64(buf b) str

The *Base64* function converts a value of the *buf* type into a string in **base64** encoding and returns it.

### DecodeInt(buf b, int offset) int

The *DecodeInt* function gets an integer from a parameter of *buf* type. *offset* is the offset in the buffer at which to read the number. The function reads 8 bytes and returns them as an integer.

### Del(buf b, int off, int length) buf

The *Del* function removes part of the data from the byte array. *off* is the offset of the data to be deleted, *length* is the number of bytes to be deleted. If *length* is less than zero, then the data will be deleted to the left of the specified offset. The function returns the *b* variable in which the deletion occurred.

### EncodeInt(buf b, int i) buf

The *EncodeInt* function appends an integer number to a specified variable of *buf* type. Since the *int* value occupies 8 bytes, 8 bytes are appended to the buffer regardless of the *i* parameter value. The function returns the *b* parameter.

### Hex(buf b) str

The *Hex* function encodes a *buf* value to a hexadecimal string and returns it.

### Insert(buf b, int off, buf src) buf

The *Insert* function inserts an array of bytes *src* into the array *b*. *off* is the offset where the specified byte array will be inserted. The function returns the variable *b*.

### SetLen(buf b, int size) buf

The *SetLen* function sets the size of the buffer. If *size* is less than the size of the buffer, then it will be truncated. Otherwise, the buffer will be padded with zeros to the specified size.

### Subbuf(buf b, int off, int length) buf

The *Subbuf* function returns a new buffer that contains the chunk of the *b* buffer with the specified offset and length.

### UnBase64(str s) buf

The *UnBase64* function converts a string in **base64** encoding into a value of the *buf* type and returns it.

### UnHex(str s) buf

The *UnHex* function returns the *buf* value represented by the hexadecimal string *s*. The input string must contain only hexadecimal characters.

### Write(buf b, int off, buf src) buf

The *Write* function writes the byte array of the *src* variable into the *b* variable starting from the specified offset. The data is written over existing values. The function returns variable *b*.


---

# Agent Instructions: 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:

```
GET https://docs.gentee.org/stdlib/buffer.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
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.
