开发者

strtoull for arbitrary length strings in C++

I have a program that accepts a string representing a number up to 256 bits in length, which I need to extend to 512 bits. The string represents a number in hexadecimal.

The way it works is, I have functions for converting strings to 64-bit, 128-bit or 256-bit numbers, where the 128-bit function calls the 64-bit function twice, the 256-bit function calls the 128-bit one twice, and the 64-bit one either uses strotull or manually converts the string.

Values greater than 64 bits are stored in an array of 64-bit entries.

This is ugly, hard to understand and bug prone, and I want to throw it all out. Considering that strtoull already handles 64-bit values nicely, I was wondering if there's some standard library that will take an arbitrary length string and convert it to an array of numbers.

This seems like a pretty basic string parsing operation, and I'm reluctant to reinvent the wheel.


Note:

Due to licensing issues (I work at a big corporation, far removed from purchasing or legal decision making), I need to use only standard libraries (GNU, Boost, STL etc...) or else it will sim开发者_StackOverflowply take me too long to get access to the solution.


There are many libraries available for handling large numbers. The first one that came up on Google was GMP. It's LGPL, so there shouldn't be any licensing issues.

mpz_t i;
mpz_init_set_str (i, "0123456789abcdef0123456789abcdef...", 16);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜