Skip to Content

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);

To report this post you need to login first.

1 Comment

You must be Logged on to comment or reply to a post.

Leave a Reply