Bitcoin ABC  0.26.3
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 
10 template <unsigned int BITS>
11 base_blob<BITS>::base_blob(const std::vector<uint8_t> &vch) {
12  assert(vch.size() == sizeof(m_data));
13  memcpy(m_data, vch.data(), sizeof(m_data));
14 }
15 
16 template <unsigned int BITS> std::string base_blob<BITS>::GetHex() const {
17  uint8_t m_data_rev[WIDTH];
18  for (int i = 0; i < WIDTH; ++i) {
19  m_data_rev[i] = m_data[WIDTH - 1 - i];
20  }
21  return HexStr(m_data_rev);
22 }
23 
24 template <unsigned int BITS> void base_blob<BITS>::SetHex(const char *psz) {
25  memset(m_data, 0, sizeof(m_data));
26 
27  // skip leading spaces
28  while (IsSpace(*psz)) {
29  psz++;
30  }
31 
32  // skip 0x
33  if (psz[0] == '0' && ToLower(psz[1]) == 'x') {
34  psz += 2;
35  }
36 
37  // hex string to uint
38  size_t digits = 0;
39  while (::HexDigit(psz[digits]) != -1) {
40  digits++;
41  }
42 
43  uint8_t *p1 = (uint8_t *)m_data;
44  uint8_t *pend = p1 + WIDTH;
45  while (digits > 0 && p1 < pend) {
46  *p1 = ::HexDigit(psz[--digits]);
47  if (digits > 0) {
48  *p1 |= uint8_t(::HexDigit(psz[--digits])) << 4;
49  p1++;
50  }
51  }
52 }
53 
54 template <unsigned int BITS>
55 void base_blob<BITS>::SetHex(const std::string &str) {
56  SetHex(str.c_str());
57 }
58 
59 // Explicit instantiations for base_blob<160>
60 template base_blob<160>::base_blob(const std::vector<uint8_t> &);
61 template std::string base_blob<160>::GetHex() const;
62 template std::string base_blob<160>::ToString() const;
63 template void base_blob<160>::SetHex(const char *);
64 template void base_blob<160>::SetHex(const std::string &);
65 
66 // Explicit instantiations for base_blob<256>
67 template base_blob<256>::base_blob(const std::vector<uint8_t> &);
68 template std::string base_blob<256>::GetHex() const;
69 template std::string base_blob<256>::ToString() const;
70 template void base_blob<256>::SetHex(const char *);
71 template void base_blob<256>::SetHex(const std::string &);
72 
73 const uint256 uint256::ZERO(0);
74 const uint256 uint256::ONE(1);
void SetHex(const char *psz)
Definition: uint256.cpp:24
std::string ToString() const
Definition: uint256.h:80
std::string GetHex() const
Definition: uint256.cpp:16
constexpr base_blob()
Definition: uint256.h:25
256-bit opaque blob.
Definition: uint256.h:129
static const uint256 ONE
Definition: uint256.h:135
static const uint256 ZERO
Definition: uint256.h:134
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:104
assert(!tx.IsCoinBase())