ProtoCore v0.0.1
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
hex.h File Reference

Base-16 conversion between raw bytes and their ASCII digits. More...

#include <stdint.h>

Go to the source code of this file.

Functions

char pc_hex_digit (uint8_t nibble, bool upper=false)
 Low nibble of nibble as a hex character, uppercase when upper.
 
int8_t pc_hex_val (char c)
 Hex character c as 0..15, or -1 when c is not a hex digit of either case.
 
uint8_t pc_hex_u32 (uint32_t v, char *out)
 Write v into out as lowercase hex digits, most significant first, and return the digit count (1..8; zero renders as a single "0").
 
void pc_hex_encode (const uint8_t *in, uint32_t n, char *out, bool upper=false)
 Write n bytes of in into out as 2 * n hex characters plus a NUL.
 
int32_t pc_hex_decode (const char *in, uint32_t hexlen, uint8_t *out, uint32_t out_cap)
 Read exactly hexlen hex characters from in into out as hexlen / 2 bytes.
 

Detailed Description

Base-16 conversion between raw bytes and their ASCII digits.

Four operations cover every hex site in the library: one nibble out, one digit in, a machine word out, and a byte run in either direction. Each writes into a caller-owned buffer, takes no heap and no <stdlib.h>, and is inline so an unused one costs nothing.

The decoders report failure through a negative return rather than a sentinel digit, so a malformed byte can never be mistaken for a valid zero.

Author
Douglas Quigg (dstroy0)
Date
2026

Definition in file hex.h.

Function Documentation

◆ pc_hex_digit()

char pc_hex_digit ( uint8_t  nibble,
bool  upper = false 
)
inline

Low nibble of nibble as a hex character, uppercase when upper.

Definition at line 30 of file hex.h.

◆ pc_hex_val()

int8_t pc_hex_val ( char  c)
inline

Hex character c as 0..15, or -1 when c is not a hex digit of either case.

Definition at line 36 of file hex.h.

Referenced by pc_hex_decode(), and pc_prov_form_field().

◆ pc_hex_u32()

uint8_t pc_hex_u32 ( uint32_t  v,
char *  out 
)
inline

Write v into out as lowercase hex digits, most significant first, and return the digit count (1..8; zero renders as a single "0").

No 0x prefix, no NUL, and no leading zeros, which is the form the HTTP/1.1 chunked size line takes. The width is counted before any digit is written so each digit lands at its final index, which is why no reversal buffer is needed. out needs room for 8 characters.

Definition at line 61 of file hex.h.

◆ pc_hex_encode()

void pc_hex_encode ( const uint8_t *  in,
uint32_t  n,
char *  out,
bool  upper = false 
)
inline

Write n bytes of in into out as 2 * n hex characters plus a NUL.

Parameters
upperuppercase A-F when true, else lowercase a-f.

out needs a capacity of at least 2 * n + 1; the caller owns that bound, since a byte run has no self-describing end.

Definition at line 83 of file hex.h.

◆ pc_hex_decode()

int32_t pc_hex_decode ( const char *  in,
uint32_t  hexlen,
uint8_t *  out,
uint32_t  out_cap 
)
inline

Read exactly hexlen hex characters from in into out as hexlen / 2 bytes.

Returns
the byte count written, or -1 when hexlen is odd, the result exceeds out_cap, or any character is not a hex digit. Nothing is written on the odd-length or capacity rejections; a bad digit stops the run where it is found.

Definition at line 100 of file hex.h.

References pc_hex_val().