views:

95

answers:

4

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.

+2  A: 
while number != 0:
    nextdigit = number % 10
    AddToLeft(result, convert nextdigit to char)
    number = number / 10

It's left as an exercise for the reader to handle zero and negative numbers.

Mark Ransom
A: 

Firstly, this is a tricky question because obviously it is based on platform and language.

Take Java for example. integers declared as int are actually 32-bit long.

so to represent the decimal value of 0 we should have

1000000000000000000000000000000 <== the leading 1 (or zero?) denotes it is positive or not.

Well, this is because Java store int values as half negative and half positive...

So, my guess is Java would do the following :

step1 : get the content from a chunk of 32-bit memory pointed by the "pointer" of the variable or the literal

step2 : calculate its decimal value so that big number is converted to 0

step3 : (jdk5+) use Int32.toString() to return the string literal as "0"

This might be wrong cause I have never thought question like this.

I really don't think any language will try converting the value into an array of chars, cause that is much overhead to add...

OR, to convert binary values to decimal, based on my math experience, you will calculate based on the value, not its literal repersentation :

    1    1  0  1  in binary

    1*2^3 + 1* 2^2 + 0*2^1 +1*2^0 = 13 in decimal     
Michael Mao
+1  A: 

How it's done depends on the platform. The Intel type processors for example has built in support for packed BCD (Binary Coded Decimal) arithmetics.

Let's say that the register al contains binary 00101010, decimal 42.

fushf ;store flags on the stack
std ;set decimal flag
sub bl, bl ;clear bl register
add bl, al ;add al to bl using BCD arithmetics
pop ;restore flags from stack

The bl register now contains 01000010.

The upper four bits contain 0100, or decimal 4.
The lower four bits contain 0010, or decimal 2.

To convert this into characters, extract the four bit values from the register and add 48 to get the character code for the digit.

Guffa
Really. Huh. AFAICT, neither Linux nor uclibc take advantage of that for `printf`. Sure, it wouldn't be portable, but they have architecture-specific code for all sorts of things. I guess representing ints as strings in base10 isn't enough of a hot spot to optimize for.
keturn
@keturn: Yes, it's not in use much any more now that divisions has gotten so cheap. I showed it mostly as an example of something different from the obvious solution.
Guffa
+1  A: 

The implementation of printf in the Linux kernel is pretty readable. See lib/vsprintf.c:number().

Well, okay, mostly pretty readable. do_div is a macro with assembler in it.

keturn