# hex::Decoder

<a id="decoder"></a>

# Decoder

```cpp
#include <icy/hex.h>

struct Decoder
```

[Defined in src/base/include/icy/hex.h:111](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L111)

> **Inherits:** [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-5)

Hex decoder.

## List of all members

| Name | Kind | Owner |
|------|------|-------|
| [`lastbyte`](#lastbyte) | `variable` | Declared here |
| [`Decoder`](#decoder-1) | `function` | Declared here |
| [`~Decoder`](#decoder-2) | `function` | Declared here |
| [`decode`](#decode-4) | `function` | Declared here |
| [`finalize`](#finalize-1) | `function` | Declared here |
| [`readnext`](#readnext) | `function` | Declared here |
| [`nybble`](#nybble) | `function` | Declared here |
| [`iswspace`](#iswspace) | `function` | Declared here |
| [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-6) | `function` | Inherited from [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-5) |
| [`~Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-7) | `function` | Inherited from [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-5) |
| [`decode`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decode-9) | `function` | Inherited from [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-5) |
| [`finalize`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#finalize-5) | `function` | Inherited from [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-5) |

## Inherited from [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-5)

| Kind | Name | Description |
|------|------|-------------|
| `function` | [`Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-6)  | Defaulted constructor. |
| `function` | [`~Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-7) `virtual` | Defaulted destructor. |
| `function` | [`decode`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decode-9) `virtual` | Decodes nread bytes from inbuf and writes decoded output to outbuf. |
| `function` | [`finalize`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#finalize-5) `virtual` `inline` | Flushes any buffered state and writes final output to outbuf. |

## Public Attributes

| Return | Name | Description |
|--------|------|-------------|
| `char` | [`lastbyte`](#lastbyte)  |  |

---

<a id="lastbyte"></a>

### lastbyte

```cpp
char lastbyte
```

[Defined in src/base/include/icy/hex.h:201](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L201)

## Public Methods

| Return | Name | Description |
|--------|------|-------------|
|  | [`Decoder`](#decoder-1) `inline` |  |
|  | [`~Decoder`](#decoder-2) `virtual` `inline` |  |
| `ssize_t` | [`decode`](#decode-4) `virtual` `inline` `override` | Decodes hex-encoded input to binary. Whitespace in the input is ignored. A trailing unpaired nibble is buffered and prepended on the next call. |
| `ssize_t` | [`finalize`](#finalize-1) `virtual` `inline` `override` | No-op finalizer; hex decoding has no pending output state. |
| `bool` | [`readnext`](#readnext) `inline` | Reads the next non-whitespace character from inbuf, prepending any buffered lastbyte before consuming from the stream. |
| `int` | [`nybble`](#nybble) `inline` | Converts an ASCII hex character to its 4-bit integer value. |
| `bool` | [`iswspace`](#iswspace) `inline` | Returns true if c is an ASCII whitespace character (space, CR, tab, LF). |

---

<a id="decoder-1"></a>

### Decoder

`inline`

```cpp
inline Decoder()
```

[Defined in src/base/include/icy/hex.h:113](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L113)

---

<a id="decoder-2"></a>

### ~Decoder

`virtual` `inline`

```cpp
virtual inline ~Decoder()
```

[Defined in src/base/include/icy/hex.h:117](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L117)

#### Reimplements

- [`Decoder::~Decoder`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decoder-7)

---

<a id="decode-4"></a>

### decode

`virtual` `inline` `override`

```cpp
virtual inline ssize_t decode(const char * inbuf, size_t nread, char * outbuf) override
```

[Defined in src/base/include/icy/hex.h:125](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L125)

Decodes hex-encoded input to binary. Whitespace in the input is ignored. A trailing unpaired nibble is buffered and prepended on the next call. 
#### Returns
Number of decoded bytes written to outbuf.

#### Reimplements

- [`Decoder::decode`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#decode-9)

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `inbuf` | `const char *` | Hex-encoded input buffer. |
| `nread` | `size_t` | Number of bytes to read from inbuf. |
| `outbuf` | `char *` | Destination buffer; must have capacity >= nread / 2. |

---

<a id="finalize-1"></a>

### finalize

`virtual` `inline` `override`

```cpp
virtual inline ssize_t finalize(char *) override
```

[Defined in src/base/include/icy/hex.h:152](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L152)

No-op finalizer; hex decoding has no pending output state. 
#### Returns
Always 0.

#### Reimplements

- [`Decoder::finalize`](https://auscaster.com/icey/docs/api-reference/icy-basic-Decoder.md#finalize-5)

---

<a id="readnext"></a>

### readnext

`inline`

```cpp
inline bool readnext(const char * inbuf, size_t nread, size_t & rpos, char & c)
```

[Defined in src/base/include/icy/hex.h:161](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L161)

Reads the next non-whitespace character from inbuf, prepending any buffered lastbyte before consuming from the stream. 
#### Returns
true if more input remains after c was read, false otherwise.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `inbuf` | `const char *` | Input buffer. |
| `nread` | `size_t` | Total bytes in inbuf. |
| `rpos` | `size_t &` | Current read position; advanced on each consumed byte. |
| `c` | `char &` | Output: the next non-whitespace character. |

---

<a id="nybble"></a>

### nybble

`inline`

```cpp
inline int nybble(const int n)
```

[Defined in src/base/include/icy/hex.h:181](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L181)

Converts an ASCII hex character to its 4-bit integer value. 
#### Returns
Integer value in the range [0, 15].

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `n` | `const int` | ASCII character ('0'-'9', 'a'-'f', 'A'-'F'). |

#### Exceptions

| Exception | Description |
|-----------|-------------|
| `std::runtime_error` | if n is not a valid hex character. |

---

<a id="iswspace"></a>

### iswspace

`inline`

```cpp
inline bool iswspace(const char c)
```

[Defined in src/base/include/icy/hex.h:196](https://github.com/auscaster/icey/blob/main/src/base/include/icy/hex.h#L196)

Returns true if c is an ASCII whitespace character (space, CR, tab, LF). 
#### Returns
true if c is whitespace.

#### Parameters

| Parameter | Type | Description |
|-----------|------|-------------|
| `c` | `const char` | Character to test. |
