Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Number to binary to number
Message
From
14/06/2006 09:18:10
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivia
 
 
To
14/06/2006 03:34:28
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Environment versions
Visual FoxPro:
VFP 9 SP1
Miscellaneous
Thread ID:
01128906
Message ID:
01128980
Views:
18
>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       --        && The starting number
 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)
Previous
Reply
Map
View

Click here to load this message in the networking platform