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 10206331 : std::string base_blob<BITS>::GetHex() const 12 : { 13 : uint8_t m_data_rev[WIDTH]; 14 336702826 : for (int i = 0; i < WIDTH; ++i) { 15 326496495 : m_data_rev[i] = m_data[WIDTH - 1 - i]; 16 326496495 : } 17 10206331 : return HexStr(m_data_rev); 18 : } 19 : 20 : template <unsigned int BITS> 21 473529 : void base_blob<BITS>::SetHex(const char* psz) 22 : { 23 473529 : std::fill(m_data.begin(), m_data.end(), 0); 24 : 25 : // skip leading spaces 26 473544 : while (IsSpace(*psz)) 27 15 : psz++; 28 : 29 : // skip 0x 30 473529 : if (psz[0] == '0' && ToLower((unsigned char)psz[1]) == 'x') 31 361323 : psz += 2; 32 : 33 : // hex string to uint 34 473529 : size_t digits = 0; 35 30769975 : while (::HexDigit(psz[digits]) != -1) 36 30296446 : digits++; 37 473529 : unsigned char* p1 = m_data.data(); 38 473529 : unsigned char* pend = p1 + WIDTH; 39 15621754 : while (digits > 0 && p1 < pend) { 40 15148225 : *p1 = ::HexDigit(psz[--digits]); 41 15148225 : if (digits > 0) { 42 15148193 : *p1 |= ((unsigned char)::HexDigit(psz[--digits]) << 4); 43 15148193 : p1++; 44 15148193 : } 45 : } 46 473529 : } 47 : 48 : template <unsigned int BITS> 49 78049 : void base_blob<BITS>::SetHex(const std::string& str) 50 : { 51 78049 : SetHex(str.c_str()); 52 78049 : } 53 : 54 : template <unsigned int BITS> 55 9646788 : std::string base_blob<BITS>::ToString() const 56 : { 57 9646788 : 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);