Skip to Content
Author's profile photo Magesh Subramanian

Binary to decimal conversion

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

Assigned Tags

      1 Comment
      You must be Logged on to comment or reply to a post.
      Author's profile photo Former Member
      Former Member

      Hi, Give me more information on issue