Bitcoin Core  0.18.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 <stdio.h>
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(data));
17  memcpy(data, vch.data(), sizeof(data));
18 }
19 
20 template <unsigned int BITS>
21 std::string base_blob<BITS>::GetHex() const
22 {
23  return HexStr(std::reverse_iterator<const uint8_t*>(data + sizeof(data)), std::reverse_iterator<const uint8_t*>(data));
24 }
25 
26 template <unsigned int BITS>
27 void base_blob<BITS>::SetHex(const char* psz)
28 {
29  memset(data, 0, sizeof(data));
30 
31  // skip leading spaces
32  while (IsSpace(*psz))
33  psz++;
34 
35  // skip 0x
36  if (psz[0] == '0' && ToLower(psz[1]) == 'x')
37  psz += 2;
38 
39  // hex string to uint
40  size_t digits = 0;
41  while (::HexDigit(psz[digits]) != -1)
42  digits++;
43  unsigned char* p1 = (unsigned char*)data;
44  unsigned char* pend = p1 + WIDTH;
45  while (digits > 0 && p1 < pend) {
46  *p1 = ::HexDigit(psz[--digits]);
47  if (digits > 0) {
48  *p1 |= ((unsigned char)::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 {
57  SetHex(str.c_str());
58 }
59 
60 template <unsigned int BITS>
61 std::string base_blob<BITS>::ToString() const
62 {
63  return (GetHex());
64 }
65 
66 // Explicit instantiations for base_blob<160>
67 template base_blob<160>::base_blob(const std::vector<unsigned char>&);
68 template std::string base_blob<160>::GetHex() const;
69 template std::string base_blob<160>::ToString() const;
70 template void base_blob<160>::SetHex(const char*);
71 template void base_blob<160>::SetHex(const std::string&);
72 
73 // Explicit instantiations for base_blob<256>
74 template base_blob<256>::base_blob(const std::vector<unsigned char>&);
75 template std::string base_blob<256>::GetHex() const;
76 template std::string base_blob<256>::ToString() const;
77 template void base_blob<256>::SetHex(const char*);
78 template void base_blob<256>::SetHex(const std::string&);
base_blob()
Definition: uint256.h:24
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:61
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:21
std::string HexStr(const T itbegin, const T itend)
Definition: strencodings.h:125
void SetHex(const char *psz)
Definition: uint256.cpp:27