Plateforme Level Extreme
Abonnement
Profil corporatif
Produits & Services
Support
Légal
English
Binary to/from Decimal
Message
Information générale
Forum:
Visual FoxPro
Catégorie:
Codage, syntaxe et commandes
Divers
Thread ID:
00418552
Message ID:
00418566
Vues:
35
>I am trying to convert a character field (which is really a character represetation of a binary number, for example, 01010) to decimal (in this case 10). Is there a built-in function in VFP that can do that?
>
>How about the reverse (going from decimal to binary)?

Aristotle,

There's no way to go to and from binary to decimal and the reverse in VFP, like you can with hexadecimal representation. So you have to "roll your own"
lcbin = "01010"
lnlast = LEN(lcbin)
store 0 to lnresult, lnexp
FOR lni = lnlast TO 1 STEP - 1
  lcchar = SUBSTR(lcbin, lni, 1)
  IF lcchar = "1"
    lnresult = lnresult + (2 ^ lnexp)
  ENDIF
  lnexp = lnexp + 1
NEXT
? lnresult
lcresult = ""
FOR lni = 0 TO 31
  IF BITTEST(lnresult, lni)
    lcresult = "1" + lcresult 
  ELSE
    lcresult = "0" + lcresult
  ENDIF
NEXT
? lcresult
George

Ubi caritas et amor, deus ibi est
Précédent
Suivant
Répondre
Fil
Voir

Click here to load this message in the networking platform