Dogecoin Core  1.14.2
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 "utilstrencodings.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[0], sizeof(data));
18 }
19 
20 template <unsigned int BITS>
21 std::string base_blob<BITS>::GetHex() const
22 {
23  char psz[sizeof(data) * 2 + 1];
24  for (unsigned int i = 0; i < sizeof(data); i++)
25  sprintf(psz + i * 2, "%02x", data[sizeof(data) - i - 1]);
26  return std::string(psz, psz + sizeof(data) * 2);
27 }
28 
29 template <unsigned int BITS>
30 void base_blob<BITS>::SetHex(const char* psz)
31 {
32  memset(data, 0, sizeof(data));
33 
34  // skip leading spaces
35  while (isspace(*psz))
36  psz++;
37 
38  // skip 0x
39  if (psz[0] == '0' && tolower(psz[1]) == 'x')
40  psz += 2;
41 
42  // hex string to uint
43  const char* pbegin = psz;
44  while (::HexDigit(*psz) != -1)
45  psz++;
46  psz--;
47  unsigned char* p1 = (unsigned char*)data;
48  unsigned char* pend = p1 + WIDTH;
49  while (psz >= pbegin && p1 < pend) {
50  *p1 = ::HexDigit(*psz--);
51  if (psz >= pbegin) {
52  *p1 |= ((unsigned char)::HexDigit(*psz--) << 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&);
void SetHex(const char *psz)
Definition: uint256.cpp:30
std::string ToString() const
Definition: uint256.cpp:65
std::string GetHex() const
Definition: uint256.cpp:21
base_blob()
Definition: uint256.h:25
void * memcpy(void *a, const void *b, size_t c)
signed char HexDigit(char c)