Line data Source code
1 : // Copyright (c) 2009-2010 Satoshi Nakamoto 2 : // Copyright (c) 2009-2020 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 : template <unsigned int BITS> 11 537960 : std::string base_blob<BITS>::GetHex() const 12 : { 13 : uint8_t m_data_rev[WIDTH]; 14 17749948 : for (int i = 0; i < WIDTH; ++i) { 15 17211988 : m_data_rev[i] = m_data[WIDTH - 1 - i]; 16 17211988 : } 17 537960 : return HexStr(m_data_rev); 18 : } 19 : 20 : template <unsigned int BITS> 21 89170 : void base_blob<BITS>::SetHex(const char* psz) 22 : { 23 89170 : std::fill(m_data.begin(), m_data.end(), 0); 24 : 25 : // skip leading spaces 26 89185 : while (IsSpace(*psz)) 27 15 : psz++; 28 : 29 : // skip 0x 30 89170 : if (psz[0] == '0' && ToLower((unsigned char)psz[1]) == 'x') 31 81227 : psz += 2; 32 : 33 : // hex string to uint 34 89170 : size_t digits = 0; 35 5792688 : while (::HexDigit(psz[digits]) != -1) 36 5703518 : digits++; 37 89170 : unsigned char* p1 = m_data.data(); 38 89170 : unsigned char* pend = p1 + WIDTH; 39 2940921 : while (digits > 0 && p1 < pend) { 40 2851751 : *p1 = ::HexDigit(psz[--digits]); 41 2851751 : if (digits > 0) { 42 2851739 : *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4); 43 2851739 : p1++; 44 2851739 : } 45 : } 46 89170 : } 47 : 48 : template <unsigned int BITS> 49 1022 : void base_blob<BITS>::SetHex(const std::string& str) 50 : { 51 1022 : SetHex(str.c_str()); 52 1022 : } 53 : 54 : template <unsigned int BITS> 55 531031 : std::string base_blob<BITS>::ToString() const 56 : { 57 531031 : return (GetHex()); 58 : } 59 : 60 : // Explicit instantiations for base_blob<160> 61 : template std::string base_blob<160>::GetHex() const; 62 : template std::string base_blob<160>::ToString() const; 63 : template void base_blob<160>::SetHex(const char*); 64 : template void base_blob<160>::SetHex(const std::string&); 65 : 66 : // Explicit instantiations for base_blob<256> 67 : template std::string base_blob<256>::GetHex() const; 68 : template std::string base_blob<256>::ToString() const; 69 : template void base_blob<256>::SetHex(const char*); 70 : template void base_blob<256>::SetHex(const std::string&); 71 : 72 : const uint256 uint256::ZERO(0); 73 : const uint256 uint256::ONE(1); 74 : const uint256 uint256::TWO(2);