Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function decToBin takes integer decimal returns string
- local string binary = ""
- local integer modulus
- loop
- set modulus = ModuloInteger( decimal, 2 )
- set binary = I2S( modulus ) + binary
- set decimal = ( decimal - modulus ) / 2
- exitwhen decimal == 0
- endloop
- return binary
- endfunction
- function validateBin takes string binary returns boolean
- local integer i = 0
- loop
- if ( ( SubString( binary, i, i + 1 ) != "1" ) and ( SubString( binary, i, i + 1 ) != "0" ) ) then
- return false
- endif
- exitwhen i == StringLength( binary ) - 1
- i = i + 1
- endloop
- return true
- endfunction
- function binToDec takes string binary returns integer
- local integer decimal = 0
- if ( validateBin( binary ) == true ) then
- binary = I2S( S2I( binary ) ) //Отсекаем нули в начале, если таковые имеются
- local integer u = StringLength( binary ) - 1;
- local integer i = 0;
- loop
- decimal = decimal + S2I( SubString( binary, i, i + 1 ) ) * R2I( Pow( 2, u ) )
- exitwhen i == StringLength( binary ) - 1
- i = i + 1
- u = u - 1
- endloop
- endif
- return decimal
- endfunction
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement