I’ve got a bit of a problem. In order to grow in my knowledge of C, I’ve decided to try to implement a basic bigint library.
The core of the bigint structure will be an array of 32 bit integers, chosen because they will fit in a register. This will allow me to do operations between digits that will overflow in a 64 bit integer (which will also fit in a register, as I’m on x86-64), and I can bitshift out each part of the result. I’ve implemented basic addition, and to test that it is working, I have to print the array. For my own testing purposes, it’s fine if I use printf() and output each digit in hex. I can read that just fine.
However, most people can’t read hex. As the number is stored in (essentially) base 2^32, printing is a bit of a problem. What would be a good way to convert to base 10?
EDIT:
This deals not with knowing how to convert from base to base, but about a good way to implement this. I was thinking along the lines of making another bigint with another base with conversion for printing.
First of all, you can’t do I/O in a sensible way without the basic operations(e.g. division and modulus). To provide efficient implementation of converting the bigint to base-10 string, I am researching two possible optimizations:
First, you can divide by some power of ten instead of ten exactly. What that means, you will get four base-10 digits every time you divide the number by 10000 for example.
Second, how would you choose which power of ten to divide by? 10, 100, 1000, 10000, etc…
There seems to be a good choice which is the maximum power of ten that can fit in your word(32-bit). Fortunately, you can implement division/modulus by one word much more efficiently than when it comes to two “bigint”s.
I haven’t given an implementation because I am still researching the problem in my spare time because I have implemented the basic operations in my library and I/O is the next step hopefully 😉