Bitcoin ABC 0.34.2
P2P Digital Currency
uint256.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2016 The Bitcoin Core developers
3// Distributed under the MIT software license, see the accompanying
4// file COPYING or http://www.opensource.org/licenses/mit-license.php.
5
6#include <uint256.h>
7
8#include <util/strencodings.h>
9
10template <unsigned int BITS> std::string base_blob<BITS>::GetHex() const {
11 uint8_t m_data_rev[WIDTH];
12 for (int i = 0; i < WIDTH; ++i) {
13 m_data_rev[i] = m_data[WIDTH - 1 - i];
14 }
15 return HexStr(m_data_rev);
16}
17
18template <unsigned int BITS>
19void base_blob<BITS>::SetHexDeprecated(const std::string_view str) {
20 std::fill(m_data.begin(), m_data.end(), 0);
21
22 const auto trimmed =
24
25 // Note: if we are passed a greater number of digits than would fit as bytes
26 // in m_data, we will be discarding the leftmost ones.
27 // str="12bc" in a WIDTH=1 m_data => m_data[] == "\0xbc", not "0x12".
28
29 size_t digits = 0;
30 for (const char c : trimmed) {
31 if (::HexDigit(c) == -1) {
32 break;
33 }
34 ++digits;
35 }
36
37 uint8_t *p1 = m_data.data();
38 uint8_t *pend = p1 + WIDTH;
39 while (digits > 0 && p1 < pend) {
40 *p1 = ::HexDigit(trimmed[--digits]);
41 if (digits > 0) {
42 *p1 |= uint8_t(::HexDigit(trimmed[--digits])) << 4;
43 p1++;
44 }
45 }
46}
47
48// Explicit instantiations for base_blob<160>
49template std::string base_blob<160>::GetHex() const;
50template std::string base_blob<160>::ToString() const;
51template void base_blob<160>::SetHexDeprecated(std::string_view);
52
53// Explicit instantiations for base_blob<256>
54template std::string base_blob<256>::GetHex() const;
55template std::string base_blob<256>::ToString() const;
56template void base_blob<256>::SetHexDeprecated(std::string_view);
57
58const uint256 uint256::ZERO(0);
59const uint256 uint256::ONE(1);
std::string ToString() const
Definition: uint256.h:117
void SetHexDeprecated(std::string_view str)
Unlike FromHex this accepts any invalid input, thus it is fragile and deprecated.
Definition: uint256.cpp:19
std::string GetHex() const
Definition: uint256.cpp:10
256-bit opaque blob.
Definition: uint256.h:199
static const uint256 ONE
Definition: uint256.h:211
static const uint256 ZERO
Definition: uint256.h:210
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:30
signed char HexDigit(char c)
Definition: hex_base.cpp:64
std::string_view TrimStringView(std::string_view str, std::string_view pattern=" \f\n\r\t\v")
Definition: string.h:74
std::string_view RemovePrefixView(std::string_view str, std::string_view prefix)
Definition: string.h:89