Skip to main content

byte_conversions.c (Byte Util Conversions)

This file implements low-level byte and string conversion utilities for MicroSui.

It provides deterministic helpers to convert between hexadecimal strings, raw byte buffers, and Base64-encoded strings. These utilities are used across the library to serialize and deserialize cryptographic data, signatures, and transaction payloads in formats required by the Sui ecosystem.

All functions are designed to be lightweight, allocation-free (unless explicitly stated), and suitable for embedded and resource-constrained environments.

File Location

Defined in microsui-lib/src/microsui_core/byte_conversions.c


hex_to_bytes

Description

Convert a hexadecimal string into its corresponding byte representation.

The function parses the input hex string (two characters per byte) and writes the decoded bytes into the provided output buffer. The conversion stops after bytes_len bytes have been written.

No dynamic memory allocation is performed. The caller is responsible for providing a sufficiently sized output buffer.

void hex_to_bytes(const char* hex_str, uint8_t* bytes, uint32_t bytes_len)

Parameters

NameTypeDescription
hex_strconst char *Input Hexadecimal character string
bytesuint8_t *Output bytes array buffer for the converted data in bytes format
bytes_lenuint32_tInput size of the bytes buffer array

Example usage

Given:

  • A defined hex_str: Data in hexadecimal string format, in this example hex_str is a Singature in Hexadecimal format.

You can convert the Hexa string in bytes as follows:

uint8_t output_signature_bytes_from_hex[97]  // We initialize it empty

hex_to_bytes(hex_str, output_signature_bytes_from_hex, sizeof(output_signature_bytes_from_hex));

bytes_to_hex

Description

Convert a byte buffer into a lowercase hexadecimal string.

Each input byte is encoded as two hexadecimal characters. The resulting string is written to the provided output buffer and is null-terminated.

The caller must ensure that the output buffer is large enough to hold bytes_len * 2 + 1 characters.

void bytes_to_hex(const uint8_t* bytes, uint32_t bytes_len, char* hex_str)

Parameters

NameTypeDescription
bytesuint8_t *Input buffer for data in bytes format
bytes_lenuint32_tInput size of the input bytes data array
hex_strchar *Output string buffer for the converted data in hexa string format

Example usage

Given:

  • A defined sui_sig: Data in bytes array format, in this example sui_sig is a Singature in bytes array format.

You can convert the Hexa string in bytes as follows:

char sui_sig_hex[195]; // 2 hex chars per byte + null terminator

bytes_to_hex(sui_sig, 97, sui_sig_hex); // 97 bytes is the length of a Sui Signature

bytes_to_base64

Description

Encode a byte buffer into a Base64-encoded string.

The function converts the input byte array into a Base64 representation and writes the result into the provided output buffer, including a null terminator.
The output is compatible with standard Base64 encoding as required by Sui RPC interfaces.

Returns an error if the output buffer is too small or if encoding fails.

int bytes_to_base64(const uint8_t* input, size_t input_len, char* output, size_t output_size)

Parameters

NameTypeDescription
inputuint8_t *Input buffer for data in bytes format
input_lensize_tInput size of the input bytes data array
outputchar *Output string buffer for the converted data in base64 string format
output_lensize_tInput size of the buffer output

Returns

  • 0 on success.
  • 1 on error.

Example usage

Given:

  • A defined input: Data in bytes array format, in this example input is a Singature in bytes array format.

You can convert the bytes array in a base64 String as follows:

char output_signature_base64[((97 + 2) / 3 * 4) + 1]; // Base64 output size calculation + 1 for null terminator

int status = bytes_to_base64(sui_sig, sizeof(sui_sig), output_sig_base64, sizeof(output_sig_base64));

if (status == 0)
// Converted signature is now stored in output_signature_base64

base64_to_bytes

int base64_to_bytes(const char* input, size_t input_len, uint8_t* output, size_t output_size)

Description

Decode a Base64-encoded string into its raw byte representation.

The function parses the input Base64 string and writes the decoded bytes into the provided output buffer. The caller must ensure the output buffer is large enough to hold the decoded data.

Returns an error if decoding fails due to invalid input or insufficient buffer size.

Parameters

NameTypeDescription
inputchar *Input buffer for data in string base64 format
input_lensize_tInput size of the input string
outputuint8_t *Output bytes buffer for the converted data in bytes format
output_lensize_tInput size of the buffer output

Returns

  • 0 on success.
  • 1 on error.

Example usage

Given:

  • A defined input sig_base64: Data in bytes array format, in this example input is a Singature in base64 string.

You can convert the base64 string in a bytes array as follows:

uint8_t output_sig_bytes[SIG_BASE64_LEN]; // Provide a large enough buffer to prevent overflow. Typically, `(sig_base64_len / 4) * 3` is sufficient (but the exact value depends on other factors).

status = base64_to_bytes(sig_base64, strlen(sig_base64), output_sig_bytes, strlen(output_sig_bytes));

if status == 0:
// Converted signature is now stored in output_sig_bytes