1

I have a string number ranging in uint256, such as "115792089237316195423570985008687907853269984665640564039457584007913129639935". I want to store the bytes of this number into a vector<unsigned char>. That is, I want to get 0xffffff...fff (256bit) stored in the vector, where the vector's size will not be larger than 32 bytes.

I have tried the following ways:

  1. Using int to receive the string number and transfer, but the number is out of the int range;

  2. Using boost::multiprecision::number<boost::multiprecision::cpp_int_backend<256, 256, boost::multiprecision::unsigned_magnitude, boost::multiprecision::unchecked, void>>. But I do not know how to transfer the string number to this type. I cannot find the details of using this type on the Internet.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
Jocelyn
  • 45
  • 1
  • 8
  • A possible solution might be to first convert the long decimal number into the corresponding hexadecimal value, still as a string. Once you have the hexadecimal representation in a string, it's easy to convert every two digits into a byte-sized integer value to store in the vector. – Some programmer dude Dec 03 '21 at 09:03
  • You just construct the boost number from a string, or stream with >>. – n. m. could be an AI Dec 03 '21 at 09:13
  • @Someprogrammerdude However, in the procedure of converting the decimal string number to the hex string number, a large integer type must be needed to receive the decimal string for converting. Then it returns back to the initial problem. – Jocelyn Dec 03 '21 at 09:15
  • @n.1.8e9-where's-my-sharem Could you please detail it in the answer? – Jocelyn Dec 03 '21 at 09:18
  • in c# but the algorithm is the same https://stackoverflow.com/questions/2652760/how-to-convert-a-gi-normous-integer-in-string-format-to-hex-format-c/18231860#18231860, also see https://stackoverflow.com/questions/45262037/algorithm-for-converting-large-hex-numbers-into-decimal-form-base-10-form – Alan Birtles Dec 03 '21 at 09:24
  • `0xffffff...fff` is 256 bits, not 255 – phuclv Dec 03 '21 at 09:47
  • @phuclv Sorry. I updated the question. – Jocelyn Dec 04 '21 at 04:07
  • If this is the *only* multiple / extended precision algorithm needed, dependency on a multiple precision library will be more effort than a robust, one-time conversion function. Consider semantics similar to: [std::stoul](https://en.cppreference.com/w/cpp/string/basic_string/stoul); Furthermore - if other multiple-precision integer arithmetic operations are required, consider GMP and its [C++ interface](https://gmplib.org/manual/C_002b_002b-Class-Interface) rather than the over-engineered [dependency hell](https://en.wikipedia.org/wiki/Dependency_hell) of the Boost libraries. – Brett Hale Dec 23 '21 at 15:10

1 Answers1

2

This Boost.Multiprecision-based solution worked for me well:

std::string input { "115792089237316195423570985008687907853269984665640564039457584007913129639935" };

boost::multiprecision::uint256_t i { input };

std::stringstream ss;
ss << std::hex << i;
std::string s = ss.str();
std::cout << s << std::endl;

std::vector<unsigned char> v{s.begin(), s.end()};    

It prints:

ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff

Is it what you are looking for?

Live demo: https://godbolt.org/z/cW1hf61Wf.

EDIT

I might have originally misunderstood the question. If you want the vector to contain the binary representation of that number (that is to serialize that number into a vector), it is also possible, and even easier:

std::string input{ "115792089237316195423570985008687907853269984665640564039457584007913129639935" };
boost::multiprecision::uint256_t i { input };
    
std::vector<unsigned char> v;
export_bits(i, std::back_inserter(v), 8);

Live demo: https://godbolt.org/z/c1GvfndG9.

Corresponding documentation: https://www.boost.org/doc/libs/1_65_0/libs/multiprecision/doc/html/boost_multiprecision/tut/import_export.html.

Daniel Langr
  • 22,196
  • 3
  • 50
  • 93
  • 2
    And if OP wants to export the bits instead, there's [export_bits](https://www.boost.org/doc/libs/1_67_0/libs/multiprecision/doc/html/boost_multiprecision/tut/import_export.html). –  Dec 03 '21 at 09:42
  • @DanielLangr Thanks very much! Your solutions address my problem perfectly!! – Jocelyn Dec 04 '21 at 02:32