Skip to content

Commit 52787ef

Browse files
committed
Add to_base16_hi_character/to_base16_lo_character.
1 parent c7548ec commit 52787ef

1 file changed

Lines changed: 17 additions & 7 deletions

File tree

include/bitcoin/system/impl/radix/base_16.ipp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -42,11 +42,21 @@ constexpr bool is_between(uint8_t value, uint8_t low, uint8_t high) NOEXCEPT
4242
return low <= value && value <= high;
4343
}
4444

45-
constexpr char to_base16_character(char digit) NOEXCEPT
45+
constexpr char to_base16_character(uint8_t digit) NOEXCEPT
4646
{
4747
return (is_between(digit, 0, 9) ? '0' : 'a' - '\xa') + digit;
4848
}
4949

50+
constexpr char to_base16_hi_character(uint8_t octet) NOEXCEPT
51+
{
52+
return to_base16_character(shift_right(octet, to_half(byte_bits)));
53+
}
54+
55+
constexpr char to_base16_lo_character(uint8_t octet) NOEXCEPT
56+
{
57+
return to_base16_character(bit_and(octet, 0x0f_u8));
58+
}
59+
5060
constexpr uint8_t from_base16_characters(char high, char low) NOEXCEPT
5161
{
5262
const auto from_base16_digit = [](char character) NOEXCEPT
@@ -99,10 +109,10 @@ constexpr std::string encode_base16(const data_slice& data) NOEXCEPT
99109
out.resize(data.size() * octet_width);
100110
auto digit = out.begin();
101111

102-
for (const auto byte: data)
112+
for (const auto octet: data)
103113
{
104-
*digit++ = to_base16_character(shift_right(byte, to_half(byte_bits)));
105-
*digit++ = to_base16_character(bit_and(byte, 0x0f_u8));
114+
*digit++ = to_base16_hi_character(octet);
115+
*digit++ = to_base16_lo_character(octet);
106116
}
107117

108118
return out;
@@ -115,10 +125,10 @@ constexpr std::string encode_hash(const data_slice& hash) NOEXCEPT
115125
out.resize(hash.size() * octet_width);
116126
auto digit = out.begin();
117127

118-
for (const auto byte: std::views::reverse(hash))
128+
for (const auto octet: std::views::reverse(hash))
119129
{
120-
*digit++ = to_base16_character(shift_right(byte, to_half(byte_bits)));
121-
*digit++ = to_base16_character(bit_and(byte, 0x0f_u8));
130+
*digit++ = to_base16_hi_character(octet);
131+
*digit++ = to_base16_lo_character(octet);
122132
}
123133

124134
return out;

0 commit comments

Comments
 (0)