r/computerscience • • 10d ago

Discussion How are arbitrary precision integers converted into ASCII when printing?

I'm just wondering how this is done algorithmically speaking. It makes sense to just map each value to an ASCII digit, until you get to the value of 10 and above, which is something I haven't thought about until now.

I am thinking about an arbitrary precision unsigned integer in this question.

Edit: thank you all for your answers!

16 Upvotes

26 comments sorted by

View all comments

3

u/Radiant64 9d ago

Naïve, simplified approach: take the value modulo 10, print the corresponding digit, divide the remaining value by ten, move cursor one step to the left, repeat until value is 0.

1

u/somewhereAtC 9d ago

This is a very common implementation and binary algorithms naturally obtain the modulo as the remainder of a division. Most compilers will then recognize when one line of code does the modulo and the next does the division, and employ the combined operation.