PIVX Core  5.6.99
P2P Digital Currency
uint256.cpp
Go to the documentation of this file.
1 // Copyright (c) 2009-2010 Satoshi Nakamoto
2 // Copyright (c) 2009-2021 The Bitcoin developers
3 // Copyright (c) 2017-2021 The PIVX Core developers
4 // Distributed under the MIT/X11 software license, see the accompanying
5 // file COPYING or http://www.opensource.org/licenses/mit-license.php.
6 
7 #include "uint256.h"
8 
9 #include "utilstrencodings.h"
10 
11 #include <string.h>
12 
13 template <unsigned int BITS>
14 base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
15 {
16  assert(vch.size() == sizeof(m_data));
17  memcpy(m_data, vch.data(), sizeof(m_data));
18 }
19 
20 template <unsigned int BITS>
21 std::string base_blob<BITS>::GetHex() const
22 {
23  uint8_t m_data_rev[WIDTH];
24  for (int i = 0; i < WIDTH; ++i) {
25  m_data_rev[i] = m_data[WIDTH - 1 - i];
26  }
27  return HexStr(m_data_rev);
28 }
29 
30 template <unsigned int BITS>
31 void base_blob<BITS>::SetHex(const char* psz)
32 {
33  memset(m_data, 0, sizeof(m_data));
34 
35  // skip leading spaces
36  while (isspace(*psz))
37  psz++;
38 
39  // skip 0x
40  if (psz[0] == '0' && tolower(psz[1]) == 'x')
41  psz += 2;
42 
43  // hex string to uint
44  size_t digits = 0;
45  while (::HexDigit(psz[digits]) != -1)
46  digits++;
47  unsigned char* p1 = (unsigned char*)m_data;
48  unsigned char* pend = p1 + WIDTH;
49  while (digits > 0 && p1 < pend) {
50  *p1 = ::HexDigit(psz[--digits]);
51  if (digits > 0) {
52  *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4);
53  p1++;
54  }
55  }
56 }
57 
58 template <unsigned int BITS>
59 void base_blob<BITS>::SetHex(const std::string& str)
60 {
61  SetHex(str.c_str());
62 }
63 
64 template <unsigned int BITS>
65 std::string base_blob<BITS>::ToString() const
66 {
67  return GetHex();
68 }
69 
70 // Explicit instantiations for base_blob<160>
71 template base_blob<160>::base_blob(const std::vector<unsigned char>&);
72 template std::string base_blob<160>::GetHex() const;
73 template std::string base_blob<160>::ToString() const;
74 template void base_blob<160>::SetHex(const char*);
75 template void base_blob<160>::SetHex(const std::string&);
76 
77 // Explicit instantiations for base_blob<256>
78 template base_blob<256>::base_blob(const std::vector<unsigned char>&);
79 template std::string base_blob<256>::GetHex() const;
80 template std::string base_blob<256>::ToString() const;
81 template void base_blob<256>::SetHex(const char*);
82 template void base_blob<256>::SetHex(const std::string&);
83 
84 // Explicit instantiations for base_blob<512>
85 template base_blob<512>::base_blob(const std::vector<unsigned char>&);
86 template std::string base_blob<512>::GetHex() const;
87 template std::string base_blob<512>::ToString() const;
88 template void base_blob<512>::SetHex(const char*);
89 template void base_blob<512>::SetHex(const std::string&);
90 
void SetHex(const char *psz)
Definition: uint256.cpp:31
std::string ToString() const
Definition: uint256.cpp:65
std::string GetHex() const
Definition: uint256.cpp:21
constexpr base_blob()
Definition: uint256.h:29
void * memcpy(void *a, const void *b, size_t c)
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
signed char HexDigit(char c)