# CompactSize.toBytes

Encodes an integer using Bitcoin's CompactSize variable-length encoding.

| Range | Encoding | Bytes | |---|---|---| | 0–252 | Direct value | 1 | | 253–65,535 | `0xFD` + 2 bytes LE | 3 | | 65,536–4,294,967,295 | `0xFE` + 4 bytes LE | 5 | | > 4,294,967,295 | `0xFF` + 8 bytes LE | 9 |

## Imports

:::code-group
```ts [Named]
import { CompactSize } from 'ox'
```

```ts [Entrypoint]
import * as CompactSize from 'ox/CompactSize'
```
:::

## Examples

```ts twoslash
import { CompactSize } from 'ox'

const bytes = CompactSize.toBytes(252)
// Uint8Array [252]

const bytes2 = CompactSize.toBytes(253)
// Uint8Array [253, 253, 0]
```

## Definition

```ts
function toBytes(
  value: bigint | number,
): Bytes.Bytes
```

**Source:** [src/core/CompactSize.ts](https://github.com/wevm/ox/blob/main/src/core/CompactSize.ts#L204)

## Parameters

### value

* **Type:** `bigint | number`

The integer to encode.

## Return Type

The CompactSize-encoded bytes.

`Bytes.Bytes`
