ProofSystem/include/ProofSystem/util.hpp¶
Utilities functions header file. More...
Namespaces¶
| Name |
|---|
| util |
Functions¶
| Name | |
|---|---|
| std::string | to_string(const std::vector< unsigned char > & bytes) Convert a byte array to a hexadecimal string. |
| bool | isLittleEndian() Checks if the architecture is little endian. |
| template <typename T > T |
HexASCII2Num(const char * p_char, std::size_t num_nibbles_resolution =sizeof(T) *2) Converts a hexadecimal ASCII char array into a number. |
| template <typename T > std::vector< T > |
HexASCII2NumStr(std::string_view string) Converts a hexadecimal ASCII char array into a vector of numbers. |
| template <typename T > std::enable_if< std::is_same< typenameT::value_type, uint8_t >::value >::type |
AdjustEndianess(T & data, std::optional< typename T::iterator > start =std::nullopt, std::optional< typename T::iterator > finish =std::nullopt) Adjust endianess if needed. |
Detailed Description¶
Utilities functions header file.
Date: 2024-01-12 Super Genius ([email protected]) Henrique A. Klein ([email protected])
Functions Documentation¶
function to_string¶
Convert a byte array to a hexadecimal string.
Parameters:
- bytes A vector of bytes to be converted.
Return: A hexadecimal string representation of the bytes.
function isLittleEndian¶
Checks if the architecture is little endian.
Return: true if little endian, false otherwise
function HexASCII2Num¶
template <typename T >
static T HexASCII2Num(
const char * p_char,
std::size_t num_nibbles_resolution =sizeof(T) *2
)
Converts a hexadecimal ASCII char array into a number.
Parameters:
- p_char Hexadecimal ASCII char array
- num_nibbles_resolution How many nibbles will constitute a number
Template Parameters:
- T uint8_t, uint16_t, uint32_t or uint64_t
Return: The converted number (8-64 bit variable)
function HexASCII2NumStr¶
Converts a hexadecimal ASCII char array into a vector of numbers.
Parameters:
- p_char Hexadecimal ASCII char array
- char_ptr_size Size of the char array
Template Parameters:
- T uint8_t, uint16_t, uint32_t or uint64_t
Return: The vector of converted numbers
function AdjustEndianess¶
template <typename T >
static std::enable_if< std::is_same< typenameT::value_type, uint8_t >::value >::type AdjustEndianess(
T & data,
std::optional< typename T::iterator > start =std::nullopt,
std::optional< typename T::iterator > finish =std::nullopt
)
Adjust endianess if needed.
Parameters:
- data The container of data (vector/array)
- start Optional beginning of the valid data
- finish Optional ending of the valid data
Template Parameters:
- T std::vector
or std::array
Source code¶
#ifndef PROOFSYSTEM_UTIL_HPP
#define PROOFSYSTEM_UTIL_HPP
#include <string>
#include <vector>
#include <optional>
#include <algorithm>
#include <cstdint>
namespace util
{
static std::string to_string( const std::vector<unsigned char> &bytes )
{
std::string out_str;
char temp_buf[3];
for ( auto it = bytes.rbegin(); it != bytes.rend(); ++it )
{
snprintf( temp_buf, sizeof( temp_buf ), "%02x", *it );
out_str.append( temp_buf, sizeof( temp_buf ) - 1 );
}
return out_str;
}
static bool isLittleEndian()
{
std::uint32_t num = 1;
std::uint8_t *bytePtr = reinterpret_cast<std::uint8_t *>( &num );
return ( *bytePtr == 1 );
}
template <typename T>
static T HexASCII2Num( const char *p_char, std::size_t num_nibbles_resolution = sizeof( T ) * 2 )
{
T sum = 0;
for ( std::int32_t i = 0; i < static_cast<int32_t>(num_nibbles_resolution); ++i )
{
if ( std::isdigit( p_char[i] ) )
{
sum += ( ( p_char[i] - '0' ) << ( 4 * ( num_nibbles_resolution - i - 1 ) ) );
}
else
{
sum += ( ( std::toupper( p_char[i] ) - 'A' + 10 ) << ( 4 * ( num_nibbles_resolution - i - 1 ) ) );
}
}
return sum;
}
template <typename T>
std::vector<T> HexASCII2NumStr( std::string_view string )
{
static_assert( std::is_same_v<T, uint8_t> || std::is_same_v<T, uint16_t> || std::is_same_v<T, uint32_t> || std::is_same_v<T, uint64_t> );
std::vector<T> out_vect;
std::size_t num_nibbles_resolution = ( sizeof( T ) * 2 );
auto point_of_insertion = [&]()
{
if ( isLittleEndian() )
{
return out_vect.begin();
}
return out_vect.end();
};
for ( std::size_t i = 0; i < string.size(); i += num_nibbles_resolution )
{
out_vect.insert( point_of_insertion(), static_cast<T>( HexASCII2Num<T>( &string[i] ) ) );
}
return out_vect;
}
template <typename T>
static typename std::enable_if<std::is_same<typename T::value_type, uint8_t>::value>::type
AdjustEndianess( T &data, std::optional<typename T::iterator> start = std::nullopt, std::optional<typename T::iterator> finish = std::nullopt )
{
if ( !start )
{
start = data.begin();
}
if ( !finish )
{
finish = data.end();
}
if ( !isLittleEndian() )
{
std::reverse( start.value(), finish.value() );
}
}
}
#endif //PROOFSYSTEM_UTIL_HPP
Updated on 2026-03-04 at 13:10:44 -0800