Skip to content

src/base/hexutil.hpp

Namespaces

Name
sgns
sgns::base

Types

Name
enum class UnhexError { NOT_ENOUGH_INPUT = 1, NON_HEX_INPUT, VALUE_OUT_OF_RANGE, MISSING_0X_PREFIX, UNKNOWN}
error codes for exceptions that may occur during unhexing

Functions

Name
std::string hex_upper(gsl::span< const uint8_t > bytes)
Converts bytes to uppercase hex representation.
std::string hex_lower(gsl::span< const uint8_t > bytes)
Converts bytes to hex representation.
outcome::result< std::vector< uint8_t > > unhex(std::string_view hex)
Converts hex representation to bytes.
outcome::result< std::vector< uint8_t > > unhexWith0x(std::string_view hex)
Unhex hex-string with 0x in the begining.
OUTCOME_HPP_DECLARE_ERROR_2(sgns::base , UnhexError )

Types Documentation

enum UnhexError

Enumerator Value Description
NOT_ENOUGH_INPUT 1
NON_HEX_INPUT
VALUE_OUT_OF_RANGE
MISSING_0X_PREFIX
UNKNOWN

error codes for exceptions that may occur during unhexing

Functions Documentation

function hex_upper

std::string hex_upper(
    gsl::span< const uint8_t > bytes
)

Converts bytes to uppercase hex representation.

Parameters:

  • bytes input bytes

Return: hexstring

function hex_lower

std::string hex_lower(
    gsl::span< const uint8_t > bytes
)

Converts bytes to hex representation.

Parameters:

  • bytes input bytes

Return: hexstring

function unhex

outcome::result< std::vector< uint8_t > > unhex(
    std::string_view hex
)

Converts hex representation to bytes.

Parameters:

  • hex hex string input

See: https://www.boost.org/doc/libs/1_51_0/libs/algorithm/doc/html/the_boost_algorithm_library/Misc/hex.html

Return: result containing array of bytes if input string is hex encoded and has even length

Note: reads both uppercase and lowercase hexstrings

function unhexWith0x

outcome::result< std::vector< uint8_t > > unhexWith0x(
    std::string_view hex
)

Unhex hex-string with 0x in the begining.

Parameters:

  • hex hex string with 0x in the beginning

Return: unhexed buffer

function OUTCOME_HPP_DECLARE_ERROR_2

OUTCOME_HPP_DECLARE_ERROR_2(
    sgns::base ,
    UnhexError 
)

Source code

#ifndef SUPERGENIUS_HEXUTIL_HPP
#define SUPERGENIUS_HEXUTIL_HPP

#include <string_view>
#include <vector>

#include <gsl/span>
#include "outcome/outcome.hpp"

namespace sgns::base {

  enum class UnhexError {
    NOT_ENOUGH_INPUT = 1,
    NON_HEX_INPUT,
    VALUE_OUT_OF_RANGE,
    MISSING_0X_PREFIX,
    UNKNOWN
  };

  std::string hex_upper(gsl::span<const uint8_t> bytes) noexcept;

  std::string hex_lower(gsl::span<const uint8_t> bytes) noexcept;

  outcome::result<std::vector<uint8_t>> unhex(std::string_view hex);

  outcome::result<std::vector<uint8_t>> unhexWith0x(std::string_view hex);
}

OUTCOME_HPP_DECLARE_ERROR_2(sgns::base, UnhexError);

#endif

Updated on 2026-03-04 at 13:10:44 -0800