#!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub dec2bin { my $bits = shift; my $str = unpack("B8", pack("c", $bits)); return $str; } sub bin2dec { return unpack("c", pack("B8", substr("0" x 8 . shift , -8))); } my @array = (127,-127); foreach $_ (@array){ if ($_ < 0) { print "Value: $_ is negative\n"; my $negative_b = dec2bin($_); print "Negative binary: ".$negative_b."\n"; my $negative_d = bin2dec($negative_b); print "Negative_binary back to decimal: ".$negative_d."\n"; } else { print "Value: $_ is possitive\n"; my $possitive_b = dec2bin($_); print "Possitive binary: ".$possitive_b."\n"; my $possitive_d = bin2dec($possitive_b); print "Possitive_binary back to decimal: ".$possitive_d."\n"; } } __END__ Value: 127 is possitive Possitive binary: 01111111 Possitive_binary back to decimal: 127 Value: -127 is negative Negative binary: 10000001 Negative_binary back to decimal: -127 #### #!/usr/bin/perl use strict; use warnings; use Data::Dumper; sub dec2bin { my $bits = shift; my $str = unpack("B8", pack("F", $bits)); return $str; } sub bin2dec { return unpack("F", pack("B8", substr("0" x 8 . shift , -8))); } my @array = (0.000008,-0.000008); foreach $_ (@array){ if ($_ < 0) { print "Value: $_ is negative\n"; my $negative_b = dec2bin($_); print "Negative binary: ".$negative_b."\n"; my $negative_d = bin2dec($negative_b); print "Negative_binary back to decimal: ".$negative_d."\n"; } else { print "Value: $_ is possitive\n"; my $possitive_b = dec2bin($_); print "Possitive binary: ".$possitive_b."\n"; my $possitive_d = bin2dec($possitive_b); print "Possitive_binary back to decimal: ".$possitive_d."\n"; } } __END__ Value: 8e-06 is possitive Possitive binary: 10001101 Use of uninitialized value $possitive_d in concatenation (.) or string at negative2bin&bin2negative.pl line 31. Possitive_binary back to decimal: Value: -8e-06 is negative Negative binary: 10001101 Use of uninitialized value $negative_d in concatenation (.) or string at negative2bin&bin2negative.pl line 24. Negative_binary back to decimal: #### #!/usr/bin/perl use strict; use warnings; my @array = (3.987654321,-3.987654321); foreach $_ (@array){ if ($_ < 0) { print "Value: $_ is negative\n"; my $negative_b = dec2bin($_,32,"f"); print "Negative binary: ".$negative_b."\n"; my $negative_d = bin2dec($negative_b,32,"f"); print "Negative_binary back to decimal: ".$negative_d."\n"; } else { print "Value: $_ is possitive\n"; my $possitive_b = dec2bin($_,32,"f"); print "Possitive binary: ".$possitive_b."\n"; my $possitive_d = bin2dec($possitive_b,32,"f"); print "Possitive_binary back to decimal: ".$possitive_d."\n"; } } sub dec2bin { my $bits = shift; my $size = shift; my $template = shift; my $str = unpack("B$size", pack($template, $bits)); print "Substring: ".$str."\n"; return $str; } sub bin2dec { my $bits = shift; my $size = shift; my $template = shift; return unpack($template, pack("B$size", substr("0" x $size . $bits , -$size))); } __END__ Value: 3.987654321 is possitive Substring: 10111010001101010111111101000000 Possitive binary: 10111010001101010111111101000000 Possitive_binary back to decimal: 3.98765420913696 Value: -3.987654321 is negative Substring: 10111010001101010111111111000000 Negative binary: 10111010001101010111111111000000 Negative_binary back to decimal: -3.98765420913696 #### #!/usr/bin/perl use strict; use warnings; my @array = (3.987654321,-3.987654321); foreach $_ (@array){ if ($_ < 0) { print "Value: $_ is negative\n"; my $negative_b = dec2bin($_,64,"F"); print "Negative binary: ".$negative_b."\n"; my $negative_d = bin2dec($negative_b,64,"F"); print "Negative_binary back to decimal: ".$negative_d."\n"; } else { print "Value: $_ is possitive\n"; my $possitive_b = dec2bin($_,64,"F"); print "Possitive binary: ".$possitive_b."\n"; my $possitive_d = bin2dec($possitive_b,64,"F"); print "Possitive_binary back to decimal: ".$possitive_d."\n"; } } sub dec2bin { my $bits = shift; my $size = shift; my $template = shift; my $str = unpack("B$size", pack($template, $bits)); print "Substring: ".$str."\n"; return $str; } sub bin2dec { my $bits = shift; my $size = shift; my $template = shift; return unpack($template, pack("B$size", substr("0" x $size . $bits , -$size))); } __END__ Value: 3.987654321 is possitive Substring: 1010111010010101000000110100111110110111111001100000111101000000 Possitive binary: 1010111010010101000000110100111110110111111001100000111101000000 Possitive_binary back to decimal: 3.987654321 Value: -3.987654321 is negative Substring: 1010111010010101000000110100111110110111111001100000111111000000 Negative binary: 1010111010010101000000110100111110110111111001100000111111000000 Negative_binary back to decimal: -3.987654321