userver: userver/utils/encoding/hex.hpp File Reference
Loading...
Searching...
No Matches
hex.hpp File Reference

Detailed Description

Encoders, decoders and helpers for hexadecimal representations.

Definition in file hex.hpp.

Go to the source code of this file.

#include <cstdint>
#include <string>
+ This graph shows which files directly or indirectly include this file:

Namespaces

namespace  utils
 Utilities.
 

Functions

char utils::encoding::ToHexChar (int num)
 Converts number to hex character; number must be within range [0,16)
 
constexpr size_t utils::encoding::FromHexUpperBound (size_t size) noexcept
 Return upper limit on number of characters required to unhex input of given size.
 
void utils::encoding::ToHex (std::string_view input, std::string &out) noexcept
 Converts input to hex and writes data to output out.
 
std::string utils::encoding::ToHex (std::string_view data) noexcept
 Allocates std::string, converts input and writes into said string.
 
std::string utils::encoding::ToHex (const void *encoded, size_t len) noexcept
 Allocates std::string, converts input and writes into said string.
 
size_t utils::encoding::FromHex (std::string_view encoded, std::string &out) noexcept
 Converts as much of input from hex as possible and writes data into out.
 
std::string utils::encoding::FromHex (std::string_view encoded) noexcept
 This FromHex overload allocates string and calls FromHex; if data is not fully a hex string, then it will be only partially processed.
 
std::string_view utils::encoding::GetHexPart (std::string_view encoded) noexcept
 
bool utils::encoding::IsHexData (std::string_view encoded) noexcept
 Checks that given range is fully a hex string. That is, if passed to FromHex, it will be fully processed.
 
std::string utils::encoding::ToHexString (uint64_t value)
 iterprets uint64_t value as array of bytes and applies ToHex to it
 
constexpr size_t utils::encoding::LengthInHexForm (size_t size) noexcept
 Calculate expected length of input after being hex encoded.
 
constexpr size_t utils::encoding::LengthInHexForm (std::string_view data) noexcept
 

Function Documentation

◆ FromHex() [1/2]

std::string utils::encoding::FromHex ( std::string_view encoded)
inlinenoexcept

This FromHex overload allocates string and calls FromHex; if data is not fully a hex string, then it will be only partially processed.

Definition at line 78 of file hex.hpp.

◆ FromHex() [2/2]

size_t utils::encoding::FromHex ( std::string_view encoded,
std::string & out )
noexcept

Converts as much of input from hex as possible and writes data into out.

To convert some range from hex, range must have even number of elements and every element must be hex character. To avoid throwing, algorithms consumes as much data as possible and returns how much it was able to process

Parameters
encodedinput range to convert
outResult will be written into out. Previous value will be cleared.
Returns
Number of characters successfully parsed.

◆ FromHexUpperBound()

constexpr size_t utils::encoding::FromHexUpperBound ( size_t size)
constexprnoexcept

Return upper limit on number of characters required to unhex input of given size.

For example:

  • FromHexUpperBound(1) = 0, because you can't really unhex one char, it is only half byte. Where is the second half?
  • FromHexUpperBound(2) = 1. Two chars will be converted into one byte
  • FromHexUpperBound(5) = 2. First 4 chars will be unhexed into 2 bytes, and there will be one left.

Definition at line 35 of file hex.hpp.

◆ GetHexPart()

std::string_view utils::encoding::GetHexPart ( std::string_view encoded)
noexcept

Returns range that constitutes hex string - e.g. sub-view of encoded that could be fully interpreted as hex string. Basically, if you have string_view a and c = GetHexPart(a), then range FromHex(c) will be fully parsed.

Parameters
encodedinput array of bytes.

◆ LengthInHexForm() [1/2]

constexpr size_t utils::encoding::LengthInHexForm ( size_t size)
constexprnoexcept

Calculate expected length of input after being hex encoded.

Definition at line 21 of file hex.hpp.

◆ LengthInHexForm() [2/2]

constexpr size_t utils::encoding::LengthInHexForm ( std::string_view data)
constexprnoexcept

Definition at line 23 of file hex.hpp.

◆ ToHex() [1/3]

std::string utils::encoding::ToHex ( const void * encoded,
size_t len )
inlinenoexcept

Allocates std::string, converts input and writes into said string.

Parameters
datastart of continuous range in memory
lensize of that range

Definition at line 58 of file hex.hpp.

◆ ToHex() [2/3]

std::string utils::encoding::ToHex ( std::string_view data)
inlinenoexcept

Allocates std::string, converts input and writes into said string.

Parameters
inputrange of input bytes

Definition at line 49 of file hex.hpp.

◆ ToHex() [3/3]

void utils::encoding::ToHex ( std::string_view input,
std::string & out )
noexcept

Converts input to hex and writes data to output out.

Parameters
inputbytes to convert
outstring to write data. out will be cleared

◆ ToHexChar()

char utils::encoding::ToHexChar ( int num)

Converts number to hex character; number must be within range [0,16)

Exceptions
out_of_boundsexception if num is out of range

◆ ToHexString()

std::string utils::encoding::ToHexString ( uint64_t value)
inline

iterprets uint64_t value as array of bytes and applies ToHex to it

Definition at line 96 of file hex.hpp.