Bitcoin ABC 0.33.12
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> void base_blob<BITS>::SetHex(const char *psz) {
19 std::fill(m_data.begin(), m_data.end(), 0);
20
21 // skip leading spaces
22 while (IsSpace(*psz)) {
23 psz++;
24 }
25
26 // skip 0x
27 if (psz[0] == '0' && ToLower(psz[1]) == 'x') {
28 psz += 2;
29 }
30
31 // hex string to uint
32 size_t digits = 0;
33 while (::HexDigit(psz[digits]) != -1) {
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(psz[--digits]);
41 if (digits > 0) {
42 *p1 |= uint8_t(::HexDigit(psz[--digits])) << 4;
43 p1++;
44 }
45 }
46}
47
48template <unsigned int BITS>
49void base_blob<BITS>::SetHex(const std::string &str) {
50 SetHex(str.c_str());
51}
52
53// Explicit instantiations for base_blob<160>
54template std::string base_blob<160>::GetHex() const;
55template std::string base_blob<160>::ToString() const;
56template void base_blob<160>::SetHex(const char *);
57template void base_blob<160>::SetHex(const std::string &);
58
59// Explicit instantiations for base_blob<256>
60template std::string base_blob<256>::GetHex() const;
61template std::string base_blob<256>::ToString() const;
62template void base_blob<256>::SetHex(const char *);
63template void base_blob<256>::SetHex(const std::string &);
64
65const uint256 uint256::ZERO(0);
66const uint256 uint256::ONE(1);
void SetHex(const char *psz)
Definition: uint256.cpp:18
std::string ToString() const
Definition: uint256.h:84
std::string GetHex() const
Definition: uint256.cpp:10
256-bit opaque blob.
Definition: uint256.h:127
static const uint256 ONE
Definition: uint256.h:133
static const uint256 ZERO
Definition: uint256.h:132
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
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:149
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.