#!/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 1.0 Initial Revision released on 06 July 2004 ################################################################################ if(@ARGV[0] eq "") { print "\n"; print "*************************************************************\n"; print "This program converts an fractional number into binary string\n"; print "*************************************************************\n"; print "\n"; print "The input string is a string of 0-9 digits\n"; print "Input is given without a '.' that means if it is desired to \n"; print "obtain a binary eq of say for example 0.7653234, then the input\n"; print "to he program is only 7653234\n"; print "The output is a 32 bit binary number\n"; print "*************************************************************\n"; print "\n"; print "ERROR: insufficient fields\n"; print "Example Usage: unix> ./point2bin.pl 7653234 \n"; #print "Binary eq of 0.7653234 is :\n"; #print "11000011111011000011101111111101\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 9 || $ele eq 8 || $ele eq 7 || $ele eq 6 || $ele eq 5 || $ele eq 4 || $ele eq 3 || $ele eq 2 || $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 = $poof2+$bin[$i]/10**($i+1); #print "poof2 = $poof2\n"; $i++; } print "The input number is being evaluated as $poof2\n"; $num = $poof2* 2**32; #print "$num\n"; $bb = dec2bin($num); print "Binary eq of $poof2 is:\n"; print "$bb\n"; }# if($error == '0') else { print "\n"; print "*************************************************************\n"; print "This program converts an fractional number into binary string\n"; print "*************************************************************\n"; print "Error! Input Not Correct."; print "String composed of 0-9s is expected\n"; print "\n"; print "Input is given without a '.' that means if it is desired to \n"; print "obtain a binary eq of say for example 0.7653234, then the input\n"; print "to he program is only 7653234\n"; print "The output is a 32 bit binary number\n"; print "Example Usage: unix> ./point2bin.pl 7653234 \n"; print "Binary Eq of 0.7653234 = 11000011111011000011101111111101\n"; print "*************************************************************\n"; print "\n"; }# else of if($error == '0') sub dec2bin { my $str = unpack("B32", pack("N", shift)); $str =~ s/^0+(?=\d)//; # otherwise you'll get leading zeros $len = length($str); $z = '0'; while ($len < 32) { $len = $len + 1; $str = $z.$str; } return $str; }