Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
Binary (base 2) - Decimal Conversion
Message
General information
Forum:
Visual FoxPro
Category:
Other
Miscellaneous
Thread ID:
00114476
Message ID:
00114535
Views:
24
>Before I implement my own conversion functions, anyone know of any built in FP functions for going from binary to decimal and back?

here you have one:

function bintodec

lparameters cadena
m.cadena = allt(m.cadena)
local m.decimal
if cadena = "0"
return 0
endif
m.decimal = 0
m.posicion = -1
do while len(m.cadena) > 0
m.lon = len(m.cadena)
m.posicion = m.posicion + 1
m.cuantas = iif(right(m.cadena, 1) = "1", 1, 0)
m.potencia = 2^m.posicion
m.decimal = m.decimal + m.potencia * m.cuantas
m.cadena = left(m.cadena, m.lon - 1)
enddo
return round(m.decimal, 0)

**************************
function dectobin

lparameters nDecimal
local cBinString
if nDecimal = 0
return "0"
endif
cBinString = ""
do while nDecimal > 0
cBinString = ;
iif(bittest(nDecimal,0),"1","0") ;
+ cBinString && Constitute righmost digit
nDecimal = bitrshift(nDecimal,1) && Drop rightmost digit
enddo
return cBinString

Hope this helps
Carlos
Saludos,
A.G.P.
---------
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform