Code archives/Algorithms/Bin2dec
This code has been declared by its author to be Public Domain code.
Download source code
| |||||
| Not sure how useful it is but I gave it to somebody else so why not... It converts a binary string to an int <edit> with a bit of error checking | |||||
SuperStrict
Print bin2dec("010011100")
Function bin2dec:Int(binval:String)
Local Val:Int = 1
Local total:Int
For Local x :Int = 0 To binval.length
Local numval:Int = Int(Mid(binval , binval.length - x , 1) )
If numval <> 0 And numval <> 1 RuntimeError "Non-binary input"
total :+ Int(Mid(binval , binval.length - x , 1) ) * Val
' debuglog Int(Mid(binval, binval.length - x , 1)) + " * " + val + " = " + total
val = Val * 2
Next
Return total
End Function |
Comments
| ||
| Then, typically, somebody suggests... Local binary$ = "10101010" Print Int("%" + binary$) (thanks Yan :) |
| ||
| How the hell? I never knew you could do it that way either! :) |
| ||
| I prefer to simply do Local i% = %10101010 |
Code Archives Forum