Bitcoin ABC  0.22.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 
10 template <unsigned int BITS>
11 base_blob<BITS>::base_blob(const std::vector<uint8_t> &vch) {
12  assert(vch.size() == sizeof(data));
13  memcpy(data, vch.data(), sizeof(data));
14 }
15 
16 template <unsigned int BITS> std::string base_blob<BITS>::GetHex() const {
17  return HexStr(std::reverse_iterator<const uint8_t *>(data + sizeof(data)),
18  std::reverse_iterator<const uint8_t *>(data));
19 }
20 
21 template <unsigned int BITS> void base_blob<BITS>::SetHex(const char *psz) {
22  memset(data, 0, sizeof(data));
23 
24  // skip leading spaces
25  while (IsSpace(*psz)) {
26  psz++;
27  }
28 
29  // skip 0x
30  if (psz[0] == '0' && ToLower(psz[1]) == 'x') {
31  psz += 2;
32  }
33 
34  // hex string to uint
35  size_t digits = 0;
36  while (::HexDigit(psz[digits]) != -1) {
37  digits++;
38  }
39 
40  uint8_t *p1 = (uint8_t *)data;
41  uint8_t *pend = p1 + WIDTH;
42  while (digits > 0 && p1 < pend) {
43  *p1 = ::HexDigit(psz[--digits]);
44  if (digits > 0) {
45  *p1 |= uint8_t(::HexDigit(psz[--digits])) << 4;
46  p1++;
47  }
48  }
49 }
50 
51 template <unsigned int BITS>
52 void base_blob<BITS>::SetHex(const std::string &str) {
53  SetHex(str.c_str());
54 }
55 
56 // Explicit instantiations for base_blob<160>
57 template base_blob<160>::base_blob(const std::vector<uint8_t> &);
58 template std::string base_blob<160>::GetHex() const;
59 template std::string base_blob<160>::ToString() const;
60 template void base_blob<160>::SetHex(const char *);
61 template void base_blob<160>::SetHex(const std::string &);
62 
63 // Explicit instantiations for base_blob<256>
64 template base_blob<256>::base_blob(const std::vector<uint8_t> &);
65 template std::string base_blob<256>::GetHex() const;
66 template std::string base_blob<256>::ToString() const;
67 template void base_blob<256>::SetHex(const char *);
68 template void base_blob<256>::SetHex(const std::string &);
69 
71  static uint256 one = uint256S(
72  "0000000000000000000000000000000000000000000000000000000000000001");
73  return one;
74 }
base_blob()
Definition: uint256.h:22
std::string ToLower(const std::string &str)
Returns the lowercase equivalent of the given string.
signed char HexDigit(char c)
uint256 & UINT256_ONE()
Definition: uint256.cpp:70
uint256 uint256S(const char *str)
uint256 from const char *.
Definition: uint256.h:131
std::string ToString() const
Definition: uint256.h:74
256-bit opaque blob.
Definition: uint256.h:120
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
Definition: strencodings.h:90
std::string GetHex() const
Definition: uint256.cpp:16
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:132
void SetHex(const char *psz)
Definition: uint256.cpp:21