> For the complete documentation index, see [llms.txt](https://stage-precision.gitbook.io/grid/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stage-precision.gitbook.io/grid/scripting/api-reference/utility-api/encoding-and-binary-data.md).

# Encoding & Binary Data

### `toBase64()`

Encodes a string as Base64.

#### Syntax

```python
sp.util.toBase64(value)
```

#### Parameters

| Parameter | Type  | Description      |
| --------- | ----- | ---------------- |
| `value`   | `str` | String to encode |

#### Returns

`str`

The Base64-encoded string.

#### Example

```python
result = sp.util.toBase64("Hello")

print(result)
```

Result:

```
SGVsbG8=
```

### `encodeHMAC_SHA1()`

Generates an HMAC-SHA1 value and returns the result as a Base64-encoded string.

#### Syntax

```python
sp.util.encodeHMAC_SHA1(message, key)
```

#### Parameters

| Parameter | Type  | Description                  |
| --------- | ----- | ---------------------------- |
| `message` | `str` | Message to encode            |
| `key`     | `str` | Secret key used for the HMAC |

#### Returns

`str`

The HMAC-SHA1 result encoded as Base64.

#### Example

```python
result = sp.util.encodeHMAC_SHA1(
    "message",
    "key"
)

print(result)
```

Result:

```
IIjfdNXyFGtIFGyvSWU3fp0L46Q=
```

### `getFloatFromBytes()`

Converts four individual byte values into a floating-point value.

The bytes are interpreted in little-endian order.

#### Syntax

```python
sp.util.getFloatFromBytes(
    byte0,
    byte1,
    byte2,
    byte3
)
```

#### Parameters

| Parameter | Type  | Description |
| --------- | ----- | ----------- |
| `byte0`   | `int` | First byte  |
| `byte1`   | `int` | Second byte |
| `byte2`   | `int` | Third byte  |
| `byte3`   | `int` | Fourth byte |

#### Returns

`float`

#### Example

```python
value = sp.util.getFloatFromBytes(
    0x00,
    0x00,
    0x80,
    0x3F
)

print(value)
```

Result:

```
1.0
```

{% hint style="info" %}
The function expects the byte values as individual integer arguments rather than as a Python `bytes`, `list` or `tuple`.
{% endhint %}

### `getInt32FromBytes()`

Converts four individual byte values into a signed 32-bit integer.

The bytes are interpreted in little-endian order.

#### Syntax

```python
sp.util.getInt32FromBytes(
    byte0,
    byte1,
    byte2,
    byte3
)
```

#### Returns

`int`

The resulting signed 32-bit integer.

#### Example

```python
value = sp.util.getInt32FromBytes(
    0x01,
    0x00,
    0x00,
    0x00
)

print(value)
```

Result:

```
1
```

Signed values are supported:

```python
value = sp.util.getInt32FromBytes(
    0xFF,
    0xFF,
    0xFF,
    0xFF
)

print(value)
```

Result:

```
-1
```

### `getInt64FromBytes()`

Converts eight individual byte values into a signed 64-bit integer.

The bytes are interpreted in little-endian order.

#### Syntax

```python
sp.util.getInt64FromBytes(
    byte0,
    byte1,
    byte2,
    byte3,
    byte4,
    byte5,
    byte6,
    byte7
)
```

#### Returns

`int`

The resulting signed 64-bit integer.

#### Example

```python
value = sp.util.getInt64FromBytes(
    0x01,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00,
    0x00
)

print(value)
```

Result:

```
1
```

Signed values are supported:

```python
value = sp.util.getInt64FromBytes(
    0xFF,
    0xFF,
    0xFF,
    0xFF,
    0xFF,
    0xFF,
    0xFF,
    0xFF
)

print(value)
```

Result:

```
-1
```

### Quick Reference

| API                                         | Returns | Description                                                   |
| ------------------------------------------- | ------- | ------------------------------------------------------------- |
| `sp.util.toBase64(value)`                   | `str`   | Encodes a string as Base64                                    |
| `sp.util.encodeHMAC_SHA1(message, key)`     | `str`   | Generates a Base64-encoded HMAC-SHA1                          |
| `sp.util.getFloatFromBytes(b0, b1, b2, b3)` | `float` | Converts four little-endian bytes to a float                  |
| `sp.util.getInt32FromBytes(b0, b1, b2, b3)` | `int`   | Converts four little-endian bytes to a signed 32-bit integer  |
| `sp.util.getInt64FromBytes(b0, ..., b7)`    | `int`   | Converts eight little-endian bytes to a signed 64-bit integer |
