You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

uint256.cpp 2.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2009-2010 Satoshi Nakamoto
  2. // Copyright (c) 2009-2016 The Starwels developers
  3. // Distributed under the MIT software license, see the accompanying
  4. // file COPYING or http://www.opensource.org/licenses/mit-license.php.
  5. #include "uint256.h"
  6. #include "utilstrencodings.h"
  7. #include <stdio.h>
  8. #include <string.h>
  9. template <unsigned int BITS>
  10. base_blob<BITS>::base_blob(const std::vector<unsigned char>& vch)
  11. {
  12. assert(vch.size() == sizeof(data));
  13. memcpy(data, &vch[0], sizeof(data));
  14. }
  15. template <unsigned int BITS>
  16. std::string base_blob<BITS>::GetHex() const
  17. {
  18. return HexStr(std::reverse_iterator<const uint8_t*>(data + sizeof(data)), std::reverse_iterator<const uint8_t*>(data));
  19. }
  20. template <unsigned int BITS>
  21. void base_blob<BITS>::SetHex(const char* psz)
  22. {
  23. memset(data, 0, sizeof(data));
  24. // skip leading spaces
  25. while (isspace(*psz))
  26. psz++;
  27. // skip 0x
  28. if (psz[0] == '0' && tolower(psz[1]) == 'x')
  29. psz += 2;
  30. // hex string to uint
  31. const char* pbegin = psz;
  32. while (::HexDigit(*psz) != -1)
  33. psz++;
  34. psz--;
  35. unsigned char* p1 = (unsigned char*)data;
  36. unsigned char* pend = p1 + WIDTH;
  37. while (psz >= pbegin && p1 < pend) {
  38. *p1 = ::HexDigit(*psz--);
  39. if (psz >= pbegin) {
  40. *p1 |= ((unsigned char)::HexDigit(*psz--) << 4);
  41. p1++;
  42. }
  43. }
  44. }
  45. template <unsigned int BITS>
  46. void base_blob<BITS>::SetHex(const std::string& str)
  47. {
  48. SetHex(str.c_str());
  49. }
  50. template <unsigned int BITS>
  51. std::string base_blob<BITS>::ToString() const
  52. {
  53. return (GetHex());
  54. }
  55. // Explicit instantiations for base_blob<160>
  56. template base_blob<160>::base_blob(const std::vector<unsigned char>&);
  57. template std::string base_blob<160>::GetHex() const;
  58. template std::string base_blob<160>::ToString() const;
  59. template void base_blob<160>::SetHex(const char*);
  60. template void base_blob<160>::SetHex(const std::string&);
  61. // Explicit instantiations for base_blob<256>
  62. template base_blob<256>::base_blob(const std::vector<unsigned char>&);
  63. template std::string base_blob<256>::GetHex() const;
  64. template std::string base_blob<256>::ToString() const;
  65. template void base_blob<256>::SetHex(const char*);
  66. template void base_blob<256>::SetHex(const std::string&);