Bitcoin Core 22.99.0
P2P Digital Currency
uint256.cpp
Go to the documentation of this file.
1// Copyright (c) 2009-2010 Satoshi Nakamoto
2// Copyright (c) 2009-2020 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
10#include <string.h>
11
12template <unsigned int BITS>
13base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
14{
15 assert(vch.size() == sizeof(m_data));
16 memcpy(m_data, vch.data(), sizeof(m_data));
17}
18
19template <unsigned int BITS>
20std::string base_blob<BITS>::GetHex() const
21{
22 uint8_t m_data_rev[WIDTH];
23 for (int i = 0; i < WIDTH; ++i) {
24 m_data_rev[i] = m_data[WIDTH - 1 - i];
25 }
26 return HexStr(m_data_rev);
27}
28
29template <unsigned int BITS>
30void base_blob<BITS>::SetHex(const char* psz)
31{
32 memset(m_data, 0, sizeof(m_data));
33
34 // skip leading spaces
35 while (IsSpace(*psz))
36 psz++;
37
38 // skip 0x
39 if (psz[0] == '0' && ToLower(psz[1]) == 'x')
40 psz += 2;
41
42 // hex string to uint
43 size_t digits = 0;
44 while (::HexDigit(psz[digits]) != -1)
45 digits++;
46 unsigned char* p1 = (unsigned char*)m_data;
47 unsigned char* pend = p1 + WIDTH;
48 while (digits > 0 && p1 < pend) {
49 *p1 = ::HexDigit(psz[--digits]);
50 if (digits > 0) {
51 *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4);
52 p1++;
53 }
54 }
55}
56
57template <unsigned int BITS>
58void base_blob<BITS>::SetHex(const std::string& str)
59{
60 SetHex(str.c_str());
61}
62
63template <unsigned int BITS>
64std::string base_blob<BITS>::ToString() const
65{
66 return (GetHex());
67}
68
69// Explicit instantiations for base_blob<160>
70template base_blob<160>::base_blob(const std::vector<unsigned char>&);
71template std::string base_blob<160>::GetHex() const;
72template std::string base_blob<160>::ToString() const;
73template void base_blob<160>::SetHex(const char*);
74template void base_blob<160>::SetHex(const std::string&);
75
76// Explicit instantiations for base_blob<256>
77template base_blob<256>::base_blob(const std::vector<unsigned char>&);
78template std::string base_blob<256>::GetHex() const;
79template std::string base_blob<256>::ToString() const;
80template void base_blob<256>::SetHex(const char*);
81template void base_blob<256>::SetHex(const std::string&);
82
83const uint256 uint256::ZERO(0);
84const uint256 uint256::ONE(1);
void SetHex(const char *psz)
Definition: uint256.cpp:30
std::string ToString() const
Definition: uint256.cpp:64
std::string GetHex() const
Definition: uint256.cpp:20
constexpr base_blob()
Definition: uint256.h:24
256-bit opaque blob.
Definition: uint256.h:124
static const uint256 ONE
Definition: uint256.h:130
static const uint256 ZERO
Definition: uint256.h:129
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
signed char HexDigit(char c)
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:121
assert(!tx.IsCoinBase())