Bitcoin ABC 0.26.3
P2P Digital Currency
Loading...
Searching...
No Matches
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 <util/strencodings.h>
9
10template <unsigned int BITS>
11base_blob<BITS>::base_blob(const std::vector<uint8_t> &vch) {
12 assert(vch.size() == sizeof(m_data));
13 memcpy(m_data, vch.data(), sizeof(m_data));
14}
15
16template <unsigned int BITS> std::string base_blob<BITS>::GetHex() const {
17 uint8_t m_data_rev[WIDTH];
18 for (int i = 0; i < WIDTH; ++i) {
19 m_data_rev[i] = m_data[WIDTH - 1 - i];
20 }
21 return HexStr(m_data_rev);
22}
23
24template <unsigned int BITS> void base_blob<BITS>::SetHex(const char *psz) {
25 memset(m_data, 0, sizeof(m_data));
26
27 // skip leading spaces
28 while (IsSpace(*psz)) {
29 psz++;
30 }
31
32 // skip 0x
33 if (psz[0] == '0' && ToLower(psz[1]) == 'x') {
34 psz += 2;
35 }
36
37 // hex string to uint
38 size_t digits = 0;
39 while (::HexDigit(psz[digits]) != -1) {
40 digits++;
41 }
42
43 uint8_t *p1 = (uint8_t *)m_data;
44 uint8_t *pend = p1 + WIDTH;
45 while (digits > 0 && p1 < pend) {
46 *p1 = ::HexDigit(psz[--digits]);
47 if (digits > 0) {
48 *p1 |= uint8_t(::HexDigit(psz[--digits])) << 4;
49 p1++;
50 }
51 }
52}
53
54template <unsigned int BITS>
55void base_blob<BITS>::SetHex(const std::string &str) {
56 SetHex(str.c_str());
57}
58
59// Explicit instantiations for base_blob<160>
60template base_blob<160>::base_blob(const std::vector<uint8_t> &);
61template std::string base_blob<160>::GetHex() const;
62template std::string base_blob<160>::ToString() const;
63template void base_blob<160>::SetHex(const char *);
64template void base_blob<160>::SetHex(const std::string &);
65
66// Explicit instantiations for base_blob<256>
67template base_blob<256>::base_blob(const std::vector<uint8_t> &);
68template std::string base_blob<256>::GetHex() const;
69template std::string base_blob<256>::ToString() const;
70template void base_blob<256>::SetHex(const char *);
71template void base_blob<256>::SetHex(const std::string &);
72
73const uint256 uint256::ZERO(0);
74const uint256 uint256::ONE(1);
void SetHex(const char *psz)
Definition uint256.cpp:24
std::string ToString() const
Definition uint256.h:80
std::string GetHex() const
Definition uint256.cpp:16
constexpr base_blob()
Definition uint256.h:25
256-bit opaque blob.
Definition uint256.h:129
static const uint256 ONE
Definition uint256.h:135
static const uint256 ZERO
Definition uint256.h:134
T GetRand(T nMax=std::numeric_limits< T >::max()) noexcept
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limi...
Definition random.h:85
constexpr bool IsSpace(char c) noexcept
Tests if the given character is a whitespace character.
std::string HexStr(const Span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
signed char HexDigit(char c)
std::string ToLower(std::string_view str)
Returns the lowercase equivalent of the given string.
assert(!tx.IsCoinBase())