>Hi All
>
>I have written my own number to binary to number function but I thought I would ask if there was an easier method. I am looking to take number eg 123456 and convert that into a binary string like "00000000000000011110001001000000". Is there a fast way to do this num to char-bin and back to number?
Others have given code samples; I will show the algorithm, as done with pencil and paper.
Example: Convert 11 (decimal) to binary. Repeteadly divide by 2, and write down the remainders, until you get a result of zero:
Result Remainder
11 --
5 1
2 1
1 0
0 1
The binary number is the column "remainder", read from the bottom to the top, in this example, 1011 (binary).
Depending on the requirements, you may want to later fill out a few zeroes to the left, e.g. 00001011.
The same algorithm can be used to convert decimal to any other base, e.g. hexadecimal.
Difference in opinions hath cost many millions of lives: for instance, whether flesh be bread, or bread be flesh; whether whistling be a vice or a virtue; whether it be better to kiss a post, or throw it into the fire... (from Gulliver's Travels)