#!/usr/bin/perl ################################################################################ # # Coypright (C) 2004 Aviral Mittal. # # All rights reserved. Reproducion in whole or in part is prohibited without # written consent of copyright Owner.The professional use of this material # is subject to the copy right owners approval in written. # ################################################################################ # # Comments welcome on avimit@yahoo.com # Visit http://www.vlsiip.com for more free stuff # ################################################################################ # Revision History # This is version 2.0 Initial Revision released on 07 July 2004 ################################################################################ # Changes w.r.t prev release # 'Notes' are added ################################################################################ # Notes: # This perl script converts a binary number into a fixed point decimal number. # The binary number is assumed to be representing the part of the fixed point # Decimal number following the '.'. For example if a fixed point decimal number # is written as abcd.wxyz, then the part .wxyz is what this script is producing. # # Example Usage: unix> ./bin2point.pl 111001 \n"; # This will produce a result as 0.890625 ################################################################################ if(@ARGV[0] eq "") { print "\n"; print "*************************************************************\n"; print "This program converts an binary string into fractional number\n"; print "The input and outputs are as per Fixed Point Format Representation\n"; print "The input string, which is a string of 0s and 1s is interpreted\n"; print "as a fractional number. The output will be therefore of the \n"; print "Form 0.xxxxxx whre 0.xxxxx = binary input string\n"; print "For example 0.890625 = 111001 \n"; print "0.890625 = 0.5*1 + 0.25*1 + 0.125*1 + 0.0625*0 + 0.03125*0 + "; print "0.015625*1\n"; print " - - - - - "; print " -\n"; print "*************************************************************\n"; print "\n"; print "ERROR: insufficient fields\n"; print "Example Usage: unix> ./bin2point.pl 10101010 \n"; print "\n"; exit; } # print "ARGV0 = $ARGV[0]\n"; $len = length($ARGV[0]); @bin = split(//,$ARGV[0]); # print "bin = @bin\n"; # print "len = $len\n"; $error = '0'; foreach $ele(@bin) { if($ele eq 1 || $ele eq 0) { #print "OK\n"; }# if($ele == '1' || $ele == '0') else { $error = '1'; } }# foreach $ele(@bin) if($error == '0') { $i = 0; $frac_out = 0.0; $poof2 = 0.0; while ($i<$len) { $poof2 = 1/2**($i+1); #print "poof2 = $poof2\n"; #print "bini = $bin[$i]\n"; $frac_out = $frac_out + $poof2*$bin[$i]; $i++; } print "$frac_out\n"; }# if($error == '0') else { print "\n"; print "*************************************************************\n"; print "This program converts an binary string into fractional number\n"; print "*************************************************************\n"; print "\n"; print "Error! Input Not Correct."; print "Binary String composed of 0s and/or 1s expected\n"; print "Example Usage: unix> ./bin2point.pl 10101010 \n"; print "0.6640625\n"; print "*************************************************************\n"; print "\n"; }# else of if($error == '0')