Raven Core  3.0.0
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 // Copyright (c) 2017-2019 The Raven Core developers
4 // Distributed under the MIT 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 <stdio.h>
12 #include <string.h>
13 
14 template <unsigned int BITS>
15 base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
16 {
17  assert(vch.size() == sizeof(data));
18  memcpy(data, vch.data(), sizeof(data));
19 }
20 
21 template <unsigned int BITS>
22 std::string base_blob<BITS>::GetHex() const
23 {
24  return HexStr(std::reverse_iterator<const uint8_t*>(data + sizeof(data)), std::reverse_iterator<const uint8_t*>(data));
25 }
26 
27 template <unsigned int BITS>
28 void base_blob<BITS>::SetHex(const char* psz)
29 {
30  memset(data, 0, sizeof(data));
31 
32  // skip leading spaces
33  while (isspace(*psz))
34  psz++;
35 
36  // skip 0x
37  if (psz[0] == '0' && tolower(psz[1]) == 'x')
38  psz += 2;
39 
40  // hex string to uint
41  const char* pbegin = psz;
42  while (::HexDigit(*psz) != -1)
43  psz++;
44  psz--;
45  unsigned char* p1 = (unsigned char*)data;
46  unsigned char* pend = p1 + WIDTH;
47  while (psz >= pbegin && p1 < pend) {
48  *p1 = ::HexDigit(*psz--);
49  if (psz >= pbegin) {
50  *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
51  p1++;
52  }
53  }
54 }
55 
56 template <unsigned int BITS>
57 void base_blob<BITS>::SetHex(const std::string& str)
58 {
59  SetHex(str.c_str());
60 }
61 
62 template <unsigned int BITS>
63 std::string base_blob<BITS>::ToString() const
64 {
65  return (GetHex());
66 }
67 
68 // Explicit instantiations for base_blob<160>
69 template base_blob<160>::base_blob(const std::vector<unsigned char>&);
70 template std::string base_blob<160>::GetHex() const;
71 template std::string base_blob<160>::ToString() const;
72 template void base_blob<160>::SetHex(const char*);
73 template void base_blob<160>::SetHex(const std::string&);
74 
75 // Explicit instantiations for base_blob<256>
76 template base_blob<256>::base_blob(const std::vector<unsigned char>&);
77 template std::string base_blob<256>::GetHex() const;
78 template std::string base_blob<256>::ToString() const;
79 template void base_blob<256>::SetHex(const char*);
80 template void base_blob<256>::SetHex(const std::string&);
base_blob()
Definition: uint256.h:26
std::string HexStr(const T itbegin, const T itend, bool fSpaces=false)
std::string ToString() const
Definition: uint256.cpp:63
void * memcpy(void *a, const void *b, size_t c)
std::string GetHex() const
Definition: uint256.cpp:22
signed char HexDigit(char c)
void SetHex(const char *psz)
Definition: uint256.cpp:28