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