# Utilities

## Hat & Tree ID Utils

Following are utility functions to handle various hat & tree ID formats. More information about hat IDs can be found [here](/for-developers/hats-protocol-for-developers/hat-ids.md).

### <mark style="color:purple;">hatIdDecimalToHex</mark>

Convert a hat ID from decimal to hex.

```typescript
import { hatIdDecimalToHex } from "@hatsprotocol/sdk-v1-core";

const hatIdHex = hatIdDecimalToHex(hatId);
```

***Arguments***:

```typescript
hatId: bigint
```

`hatId` - Hat ID in decimal format.

***Response***:

```typescript
`0x${string}`
```

Hat ID in a hex format.

### <mark style="color:purple;">hatIdHexToDecimal</mark>

Convert a hat ID from hex to decimal.

```typescript
import { hatIdHexToDecimal } from "@hatsprotocol/sdk-v1-core";

const hatIdDecimal = hatIdHexToDecimal(hatId);
```

***Arguments***:

```typescript
hatId: string
```

`hatId` - Hat ID in hex format.

***Response***:

```typescript
bigint
```

Hat ID in a decimal format.

### <mark style="color:purple;">treeIdDecimalToHex</mark>

Convert a tree ID from decimal to hex. A tree ID is the first 4 bytes in a hat ID.

```typescript
import { treeIdDecimalToHex } from "@hatsprotocol/sdk-v1-core";

const treeIdHex = treeIdDecimalToHex(treeId);
```

***Arguments***:

```typescript
treeId: number
```

`treeId` - Tree ID in decimal format.&#x20;

***Response***:

```typescript
`0x${string}`
```

Tree ID in a hex format.

### <mark style="color:purple;">treeIdHexToDecimal</mark>

Convert a tree ID from hex to decimal. A tree ID is the first 4 bytes in a hat ID.

```typescript
import { treeIdHexToDecimal } from "@hatsprotocol/sdk-v1-core";

const treeIdDecimal = treeIdHexToDecimal(treeId);
```

***Arguments***:

```typescript
treeId: string
```

`treeId` - Tree ID in hex format.&#x20;

***Response***:

```typescript
number
```

Tree ID in a decimal format.

### <mark style="color:purple;">treeIdToTopHatId</mark>

Convert a tree ID to its top-hat ID. A tree ID is the first 4 bytes in a hat ID.

```typescript
import { treeIdToTopHatId } from "@hatsprotocol/sdk-v1-core";

const tophatId = treeIdToTopHatId(treeId);
```

***Arguments***:

```typescript
treeId: number
```

`treeId` - Tree ID in decimal format.&#x20;

***Response***:

```typescript
bigint
```

Top-hat ID in decimal format.

### <mark style="color:purple;">hatIdToTreeId</mark>

Convert a hat ID to its tree ID. A tree ID is the first 4 bytes in a hat ID.

```typescript
import { hatIdToTreeId } from "@hatsprotocol/sdk-v1-core";

const treeId = hatIdToTreeId(hatId);
```

***Arguments***:

```typescript
hatId: bigint
```

`hatId` - Hat ID in decimal format.&#x20;

***Response***:

```typescript
number
```

Tree ID of the hat, in a decimal format.

### <mark style="color:purple;">hatIdDecimalToIp</mark>

The IP format may be used as a "pretty" hat ID format for presenting.&#x20;

For example, a hat with a hex ID of:\
`0x00000001000a0002000000000000000000000000000000000000000000000000`  will have an IP format of 1.10.2 - Each level is separated by a dot and presented as a decimal number, excluding zeros.&#x20;

```typescript
import { hatIdDecimalToIp } from "@hatsprotocol/sdk-v1-core";

const hatIdIp = hatIdDecimalToIp(hatId);
```

***Arguments***:

```typescript
hatId: bigint
```

`hatId` - Hat ID in decimal format.&#x20;

***Response***:

```typescript
string
```

Hat ID in IP format.

### <mark style="color:purple;">hatIdIpToDecimal</mark>

Convert a hat ID from an IP format, to a decimal format.

<pre class="language-typescript"><code class="lang-typescript"><strong>import { hatIdIpToDecimal } from "@hatsprotocol/sdk-v1-core";
</strong>
const hatIdDecimal = hatIdIpToDecimal(hatId);
</code></pre>

***Arguments***:

```typescript
hatId: string
```

`hatId` - Hat ID in IP format.&#x20;

***Response***:

```typescript
bigint
```

Hat ID in decimal format.

## Constants

Following are Hats-specific exported constant values.

<pre class="language-typescript"><code class="lang-typescript"><strong>import {   
</strong>  HATS_V1, // Hats-Protocol v1 contract address
  MAX_LEVELS, // Max levels on a Hats tree
  MAX_LEVEL_HATS, // Max amount of hats on each level (excluding level 0)
  ZERO_ID, // The zero Hat ID in hex format 
  FALLBACK_ADDRESS, // The fallback address for eligibility and toggle 
<strong>} from "@hatsprotocol/sdk-v1-core";
</strong></code></pre>


---

# 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.hatsprotocol.xyz/for-developers/v1-sdk/core/hat-and-tree-id-utilities.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.
