Scientific Calculator

(public) shiwei/bin2dec

By shiwei ryan

x = bin2dec(b) interprets the binary string B and returns in X the equivalent decimal number. Note: The first char of b should not be ZERO because of the bug of Scientific Calculator

Tagged: binary decimal

var bin2dec = function(b){

var bs = string(b);
var i = 0;
var j = len bs - 1;
var dec = 0;

while ( i < len bs ) -> (
dec += ( bs[i] - 48 ) * ( 2^j );  //bs are in ASCII code
i += 1;
j -= 1;
);
dec;
}

spam? | offensive?

1 Comment

Sign in to leave a comment