Here's a piece of code which takes the binary number as input and convert them into a decimal output
$l_input is varchar and rest of the variables are of type int.
#$l_input = '100111101111'; (=2543)
$l_len = length($l_input);
$l_lastDigit = 0;
$l_sum = 0;
$l_i =0;
while ( $l_i < $l_len)
begin
$l_lastDigit =substr($l_input,length($l_input),1) ;
$l_sum = $l_sum + $l_lastDigit * power(2,$l_i);
$l_input = substr($l_input,1,length($l_input)-1 ) ;
$l_i = $l_i + 1;
end
print ($l_sum);
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
37 | |
10 | |
6 | |
4 | |
3 | |
3 | |
3 | |
2 | |
2 | |
2 |