Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Number to binary to number
Message
De
14/06/2006 09:18:10
Hilmar Zonneveld
Independent Consultant
Cochabamba, Bolivie
 
 
À
14/06/2006 03:34:28
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Versions des environnements
Visual FoxPro:
VFP 9 SP1
Divers
Thread ID:
01128906
Message ID:
01128980
Vues:
22
>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)
Précédent
Répondre
Fil
Voir

Click here to load this message in the networking platform