How are integers converted to strings under the hood?
Posted
by CrazyJugglerDrummer
on Stack Overflow
See other posts from Stack Overflow
or by CrazyJugglerDrummer
Published on 2010-03-23T23:05:18Z
Indexed on
2010/03/23
23:13 UTC
Read the original article
Hit count: 352
I suppose the real question is how to convert base2/binary to base10. The most common application of this would probably be in creating strings for output: turning a chunk of binary numerical data into an array of characters. How exactly is this done?
my guess: Seeing as there probably isn't a string predefined for each numerical value, I'm guessing that the computer goes through each bit of the integer from right to left, each time incrementing the appropriate values in the char array/base10 notation places. If we take the number 160 in binary (10100000), it would know that a 1 in the 8th place means 128, so it places 1 into the third column, 2 in the second, and 8 in the third. The 1 in the 6th column means 32, and it would add those values to the second and first place, carrying over if needed. After this it's an easy conversion to actual char codes.
© Stack Overflow or respective owner