#!/Perl # test3.pl use strict; use warnings; # function dec2bin(dec){ # if (dec >= 0) { # return (dec >>> 0).toString(2); # } else { # return (~dec).toString(2); # } #} # (-3 >>> 0).toString(2)) # sub prt { my ($t) = shift; print "$t"; } 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))); } sub dec2bin2($) { my $dec = shift; my $res = ''; my ($rem); while ($dec >= 1) { $rem = $dec % 2; # Modulo division to get remainder $res = $rem . $res; # Concatenation of two numbers $dec = int($dec / 2); # New Value of decimal number to do next set of above operations } return $res; } sub get_sets_of_8($) { my $ra = shift; my $len = scalar @{$ra}; my $cnt = 0; my ($i,$ch); my $set = ''; my @sets = (); for ($i = ($len - 1); $i >= 0; $i--) { $ch = ${$ra}[$i]; $set = $ch.$set; $cnt++; if ($cnt == 8) { push(@sets,$set); $set = ''; $cnt = 0; } } if ($cnt) { while ($cnt < 0) { $set = '0'.$set; $cnt++; } push(@sets,$set); } @sets = reverse @sets; prt("setof8 ".join(" ",@sets)."\n"); } my $fact = 0.000005; my $lon = -179.9832104; my $lon2 = -179.9832199; my $neg = 0; if ($lon < 0) { $neg = 1; $lon *= -1; } # why 1000111110000101010010001 - inverted # 17998321 = 1000100101010000111110001 # 1 00010010 10100001 11110001 # 00000001 00010010 10100001 11110001 # 11111110 11101101 01011110 00001110 # 11111110 11101101 01011110 00001111 my $int = int( ($lon + $fact) * 100000 ); my $decimal = $int; my $dec_number = $int; my $dec = $int; my ($i,$ch,$len,$num,$rem,$pn,$remainder,$result); $len = length($int); $num = ''; $num = ''; my $num2 = ''; while($decimal >= 1) { if($decimal == 1) { $num .= 1; $num2 = ($num2.'1'); last; } $remainder = $decimal % 2; $num .= $remainder; $num2 = "$num2$remainder"; $decimal = $decimal / 2; } $len = length($num); $num2 = ''; for ($i = $len - 1; $i >= 0; $i--) { $ch = substr($num,$i,1); $num2 .= $ch; } $result = ''; while ($dec_number >= 1) { $remainder = $dec_number % 2; # Modulo division to get remainder $result = $remainder . $result; # Concatenation of two numbers $dec_number = $dec_number / 2; # New Value of decimal number to do next set of above operations } my @rem = (); $i = 0; while ($dec > 0) { $rem[$i++] = $dec % 2; $dec = int($dec / 2); } print "Binary number Output => ", reverse(@rem), "\n"; #print "Binary number Output => ", $result, "\n"; #prt( "$lon => $int => $num => $num2\n"); prt( "$lon => $int => $num2\n"); my @num3 = (); $len = length($num2); $rem = $len % 8; if ($rem) { $rem = 8 - $rem; while ($rem) { push(@num3,0); $rem--; } } if ($neg) { for ($i = 0; $i < $len; $i++) { $ch = substr($num2,$i,1); push(@num3,$ch); } # invert $len = scalar @num3; for ($i = 0; $i < $len; $i++) { $ch = $num3[$i]; if ($ch == 1) { $num3[$i] = 0; } else { $num3[$i] = 1; } } # add 1 $len = scalar @num3; while ($len) { $len--; $ch = $num3[$len]; if ($ch == 0) { $num3[$len] = 1; last; } else { $num3[$len] = 0; } } } else { for ($i = 0; $i < $len; $i++) { $ch = substr($num2,$i,1); push(@num3,$ch); } } prt( "Get binary represetation...\n"); prt( "$lon => $int => ".join("",@num3)."\n"); prt("Expect 11111110 11101101 01011110 00001111\n"); get_sets_of_8(\@num3); prt("Shift left one bit...\n"); # pop @num3; shift @num3; push(@num3,0); prt( "Expect 11111101110110101011110000011110\n"); prt( " ".join("",@num3)."\n"); $len = scalar @num3; if ($neg) { prt("Negative value to invert value...\n"); for ($i = 0; $i < $len; $i++) { $ch = $num3[$i]; if ($ch == 1) { $num3[$i] = 0; } else { $num3[$i] = 1; } } ###prt( "$lon => $int => ".join("",@num3)."\n"); prt("Expect 00000010 00100101 01000011 11100001\n"); get_sets_of_8(\@num3); } prt("Break into 5 bit chunks...\n"); $len = scalar @num3; my @chunks = (); my $cnt = 0; my $chk = ''; for ($i = ($len - 1); $i >= 0; $i--) { $ch = $num3[$i]; $chk = $ch.$chk; #$chk .= $ch; $cnt++; if ($cnt == 5) { push(@chunks,$chk); $cnt = 0; $chk = ''; } } #if ($cnt) { # push(@chunks,$chk); #} prt("Expect 00001 00010 01010 10000 11111 00001\n"); prt("5-bits ".join(" ",reverse @chunks)."\n"); prt("Place the 5-bit chunks into reverse order:\n"); prt("Expect 00001 11111 10000 01010 00010 00001\n"); prt("5-bits ".join(" ",@chunks)."\n"); prt("OR each value with 0x20 if another bit chunk follows:\n"); prt("Expect 100001 111111 110000 101010 100010 000001\n"); $len = scalar @chunks; for ($i = 0; $i < ($len - 1); $i++) { $chk = $chunks[$i]; $cnt = bin2dec($chk); $cnt |= 0x20; #$chk = dec2bin($cnt); $chk = dec2bin2($cnt); $chk = '0'.$chk while (length($chk) < 5); $chunks[$i] = $chk; } prt("5-bits ".join(" ",@chunks)."\n"); prt("Convert each value to decimal:\n"); prt("Expect 33 63 48 42 34 1\n"); $len = scalar @chunks; my @decs = (); for ($i = 0; $i < $len; $i++) { $chk = $chunks[$i]; $cnt = bin2dec($chk); #prt("$cnt "); push(@decs,$cnt); } prt("Got ".join(" ",@decs)."\n"); prt("Add 63 to each value:\n"); prt("Expect 96 126 111 105 97 64\n"); $len = scalar @decs; for ($i = 0; $i < $len; $i++) { $decs[$i] += 63; } prt("Got ".join(" ",@decs)."\n"); prt("Convert each value to its ASCII equivalent:\n"); prt("Expect `~oia\@\n"); $len = scalar @decs; $chk = ''; for ($i = 0; $i < $len; $i++) { $ch = chr($decs[$i]); $chk .= "$ch"; } prt("Got $chk\n"); # Points: (38.5, -120.2), (40.7, -120.95), (43.252, -126.453) # Encoded polyline: _p~iF~ps|U_ulLnnqC_mqNvxq`@ sub get_google_encode($) { my $pos = shift; my ($i,$ch); my $neg = 0; prt("Processing pos $pos\n"); if ($pos < 0) { $neg = 1; $pos *= -1; } my $f = 0.000005; my $num = int(( $pos + $f ) * 100000); my $num2 = dec2bin2($num); prt("num2 $num2\n"); # lloks good my @num3 = (); $len = length($num2); $rem = $len % 8; if ($rem) { $rem = 8 - $rem; while ($rem) { push(@num3,0); $rem--; } } if ($neg) { for ($i = 0; $i < $len; $i++) { $ch = substr($num2,$i,1); push(@num3,$ch); } # invert $len = scalar @num3; for ($i = 0; $i < $len; $i++) { $ch = $num3[$i]; if ($ch == 1) { $num3[$i] = 0; } else { $num3[$i] = 1; } } # add 1 $len = scalar @num3; while ($len) { $len--; $ch = $num3[$len]; if ($ch == 0) { $num3[$len] = 1; last; } else { $num3[$len] = 0; } } } else { for ($i = 0; $i < $len; $i++) { $ch = substr($num2,$i,1); push(@num3,$ch); } } prt( "Get binary represetation...\n"); prt("Expect 11111110 11101101 01011110 00001111\n"); get_sets_of_8(\@num3); prt("Shift left one bit...\n"); shift @num3; push(@num3,0); prt( "Expect 11111101110110101011110000011110\n"); prt( " ".join("",@num3)."\n"); $len = scalar @num3; if ($neg) { # INVERT #prt("Negative value to invert value...\n"); for ($i = 0; $i < $len; $i++) { $ch = $num3[$i]; if ($ch == 1) { $num3[$i] = 0; } else { $num3[$i] = 1; } } ###prt( "$lon => $int => ".join("",@num3)."\n"); prt("Expect 00000010 00100101 01000011 11100001\n"); get_sets_of_8(\@num3); } else { for ($i = 0; $i < $len; $i++) { $ch = substr($num2,$i,1); push(@num3,$ch); } } prt("Break into 5 bit chunks...\n"); $len = scalar @num3; my @chunks = (); my $cnt = 0; my $chk = ''; for ($i = ($len - 1); $i >= 0; $i--) { $ch = $num3[$i]; $chk = $ch.$chk; #$chk .= $ch; $cnt++; if ($cnt == 5) { push(@chunks,$chk); $cnt = 0; $chk = ''; } } #if ($cnt) { # push(@chunks,$chk); #} prt("Expect 00001 00010 01010 10000 11111 00001\n"); prt("5-bits ".join(" ",reverse @chunks)."\n"); prt("Place the 5-bit chunks into reverse order:\n"); prt("Expect 00001 11111 10000 01010 00010 00001\n"); prt("5-bits ".join(" ",@chunks)."\n"); prt("OR each value with 0x20 if another bit chunk follows:\n"); prt("Expect 100001 111111 110000 101010 100010 000001\n"); $len = scalar @chunks; for ($i = 0; $i < ($len - 1); $i++) { $chk = $chunks[$i]; $cnt = bin2dec($chk); $cnt |= 0x20; #$chk = dec2bin($cnt); $chk = dec2bin2($cnt); $chk = '0'.$chk while (length($chk) < 5); $chunks[$i] = $chk; } prt("5-bits ".join(" ",@chunks)."\n"); prt("Convert each value to decimal:\n"); prt("Expect 33 63 48 42 34 1\n"); $len = scalar @chunks; my @decs = (); for ($i = 0; $i < $len; $i++) { $chk = $chunks[$i]; $cnt = bin2dec($chk); #prt("$cnt "); push(@decs,$cnt); } prt("Got ".join(" ",@decs)."\n"); prt("Add 63 to each value:\n"); prt("Expect 96 126 111 105 97 64\n"); $len = scalar @decs; for ($i = 0; $i < $len; $i++) { $decs[$i] += 63; } prt("Got ".join(" ",@decs)."\n"); prt("Convert each value to its ASCII equivalent:\n"); prt("Expect `~oia\@\n"); $len = scalar @decs; $chk = ''; for ($i = 0; $i < $len; $i++) { $ch = chr($decs[$i]); $chk .= "$ch"; } prt("Got $chk\n"); return $chk; } my $enc = get_google_encode(-179.9832104); prt("Encoded point $enc\n"); sub do_array() { my @array = (127,-127,-17998321); 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"; } } } sub get_txt() { my $txt = <