Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binary to/from Decimal
Message
General information
Forum:
Visual FoxPro
Category:
Coding, syntax & commands
Miscellaneous
Thread ID:
00418552
Message ID:
00418566
Views:
36
>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
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform