News:

Function Finder  Find all the functions within source code files

Main Menu

Convert Binary String TO Dec

Started by kevin, July 15, 2003, 03:23:50 PM

Previous topic - Next topic

kevin

Convert Binary String to Decimal



` DARK BASIC VERSIONS
` ====================

For lp =0 to 5

b=rnd(255)
binary$=right$(bin$(b),8)

r=BinString_To_Dec(binary$)

print binary$
print b;"   "; r

next lp

wait key
end

Function BinString_To_Dec(binary$)
if left$(binary$,1)="%" then binary$=right$(binary$,len(binary$)-1)
result=0
For lp=len(binary$) to 1 step -1
 result=result+(val( mid$(binary$,lp) )*(2^( len(binary$) -lp)))
next lp
EndFunction result


kevin

Blitz 2D/3D version


For lp =0 To 5
b=Rnd(255)
binary$=Right$(Bin$(b),8)

r=BinString_To_Dec(binary$)

Print binary$
Print b+"   "+ r

Next
WaitKey
End


Function BinString_To_Dec(binary$)
If Left$(binary$,1)="%" Then binary$=Right$(binary$,Len(binary$)-1)
result=0
For lp=Len(binary$) To 1 Step -1
 t=Mid$(binary$,lp,1)
 result=result+(t*(2^( Len(binary$) -lp)))
Next
Return result
End Function



kevin

#2
PlayBASIC Code: [Select]
 ` PlayBASIC Version
`=================

For lp =0 to 5

b=rnd(255)
binary$="%"+right$(bin$(b),8)

r=val(binary$)

print binary$
print str$(b)+" "+str$(r)

next lp

waitkey
end