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.
8 #include "utilstrencodings.h"
13 template <unsigned int BITS
>
14 base_blob
<BITS
>::base_blob(const std::vector
<unsigned char>& vch
)
16 assert(vch
.size() == sizeof(data
));
17 memcpy(data
, vch
.data(), sizeof(data
));
20 template <unsigned int BITS
>
21 std::string base_blob
<BITS
>::GetHex() const
23 return HexStr(std::reverse_iterator
<const uint8_t*>(data
+ sizeof(data
)), std::reverse_iterator
<const uint8_t*>(data
));
26 template <unsigned int BITS
>
27 void base_blob
<BITS
>::SetHex(const char* psz
)
29 memset(data
, 0, sizeof(data
));
31 // skip leading spaces
36 if (psz
[0] == '0' && tolower(psz
[1]) == 'x')
40 const char* pbegin
= psz
;
41 while (::HexDigit(*psz
) != -1)
44 unsigned char* p1
= (unsigned char*)data
;
45 unsigned char* pend
= p1
+ WIDTH
;
46 while (psz
>= pbegin
&& p1
< pend
) {
47 *p1
= ::HexDigit(*psz
--);
49 *p1
|= ((unsigned char)::HexDigit(*psz
--) << 4);
55 template <unsigned int BITS
>
56 void base_blob
<BITS
>::SetHex(const std::string
& str
)
61 template <unsigned int BITS
>
62 std::string base_blob
<BITS
>::ToString() const
67 // Explicit instantiations for base_blob<160>
68 template base_blob
<160>::base_blob(const std::vector
<unsigned char>&);
69 template std::string base_blob
<160>::GetHex() const;
70 template std::string base_blob
<160>::ToString() const;
71 template void base_blob
<160>::SetHex(const char*);
72 template void base_blob
<160>::SetHex(const std::string
&);
74 // Explicit instantiations for base_blob<256>
75 template base_blob
<256>::base_blob(const std::vector
<unsigned char>&);
76 template std::string base_blob
<256>::GetHex() const;
77 template std::string base_blob
<256>::ToString() const;
78 template void base_blob
<256>::SetHex(const char*);
79 template void base_blob
<256>::SetHex(const std::string
&);