2012-02-29 12:05:49 -08:00
|
|
|
package DDG::Goodie::Binary;
|
2014-08-20 11:45:33 -07:00
|
|
|
# ABSTRACT: Convert decimal, hex and string to binary.
|
2012-02-29 12:05:49 -08:00
|
|
|
|
2015-02-22 12:09:29 -08:00
|
|
|
use strict;
|
2012-02-29 12:05:49 -08:00
|
|
|
use DDG::Goodie;
|
2012-05-23 19:05:08 -07:00
|
|
|
|
2014-07-28 17:45:20 -07:00
|
|
|
use Bit::Vector;
|
|
|
|
|
2012-05-23 19:05:08 -07:00
|
|
|
triggers end => "binary";
|
2012-02-29 12:05:49 -08:00
|
|
|
|
2012-03-20 21:08:12 -07:00
|
|
|
zci answer_type => "binary_conversion";
|
2014-10-15 06:16:52 -07:00
|
|
|
zci is_cached => 1;
|
2012-02-29 12:05:49 -08:00
|
|
|
|
2014-02-12 07:16:17 -08:00
|
|
|
primary_example_queries 'foo in binary', '12 as binary', 'hex 0xffff into binary';
|
2012-11-06 12:53:46 -08:00
|
|
|
secondary_example_queries '0x1e to binary';
|
|
|
|
description 'convert ASCII, numbers, and hex to binary';
|
|
|
|
name 'Binary';
|
|
|
|
code_url 'https://github.com/duckduckgo/zeroclickinfo-goodies/blob/master/lib/DDG/Goodie/Binary.pm';
|
|
|
|
category 'conversions';
|
|
|
|
topics 'geek';
|
|
|
|
|
2012-02-29 12:05:49 -08:00
|
|
|
sub bin {
|
|
|
|
my @tex = shift;
|
|
|
|
my $bin;
|
|
|
|
for(my $x = 0; $x <= $#tex; $x++) {
|
|
|
|
$bin .= unpack("B*", $tex[$x]);
|
|
|
|
}
|
|
|
|
return $bin;
|
|
|
|
}
|
|
|
|
|
2012-04-12 12:12:21 -07:00
|
|
|
sub dec2bin {
|
2014-07-28 17:45:20 -07:00
|
|
|
my $dec = shift;
|
|
|
|
# Method for packing without 'q' taken from:
|
|
|
|
# http://www.perlmonks.org/?node_id=163123
|
|
|
|
my $vec = Bit::Vector->new_Dec(64, $dec);
|
|
|
|
my $str = unpack("B*", pack('NN', $vec->Chunk_Read(32, 32), $vec->Chunk_Read(32, 0)));
|
|
|
|
$str =~ s/^0+(?=\d)//; # first suppress leading zeros
|
|
|
|
my $alignment = length($str) % 8; # Then see if we need to pad for byte-sized output.
|
|
|
|
return ($alignment) ? ('0' x (8 - $alignment)) . $str : $str;
|
2012-04-12 12:12:21 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
sub hex2bin {
|
|
|
|
dec2bin(hex(shift));
|
|
|
|
}
|
|
|
|
|
|
|
|
sub bin2dec {
|
|
|
|
my @bin = shift;
|
|
|
|
my $str = unpack("N", pack("B32", substr("0" x 32 . $bin[0], -32)));
|
|
|
|
return $str;
|
|
|
|
}
|
|
|
|
|
2012-02-29 12:05:49 -08:00
|
|
|
|
|
|
|
handle remainder => sub {
|
2012-04-22 12:40:35 -07:00
|
|
|
my @out;
|
2014-02-12 07:16:17 -08:00
|
|
|
|
2014-02-26 14:30:12 -08:00
|
|
|
if (/^([01]+)(\s+from)?$/) {
|
2014-02-12 07:16:17 -08:00
|
|
|
# Looks like they gave us some binary, let's turn it into decimal!
|
2015-01-09 00:46:54 -08:00
|
|
|
@out = ($1, bin2dec($1), "Binary", "Decimal");
|
2014-02-12 07:16:17 -08:00
|
|
|
} elsif (s/\s+(in|to|into|as)$//) {
|
|
|
|
# Looks like they are asking for a conversion to binary
|
|
|
|
# So, try to figure out what they've got.
|
|
|
|
# They can either tell us explicitly or we can try to just work it out.
|
|
|
|
if (/^(?:decimal\s+)?([0-9]+)$/) {
|
2015-01-09 00:46:54 -08:00
|
|
|
@out = ($1, dec2bin($1), "Decimal", "Binary");
|
2014-02-12 07:16:17 -08:00
|
|
|
} elsif (/^(?:hex\s+)?(?:0x|Ox|x)?([0-9a-fA-F]+)$/) {
|
|
|
|
# Normalize the hex output with lowercase and a prepended '0x'.
|
2015-01-09 00:46:54 -08:00
|
|
|
@out = ('0x' . lc $1, hex2bin($1), "Hex", "Binary");
|
2014-02-12 07:16:17 -08:00
|
|
|
} else {
|
|
|
|
# We didn't match anything else, so just convert whatever string is left.
|
2015-01-08 23:56:33 -08:00
|
|
|
@out = ($_, bin($_), "String", "Binary");
|
2014-02-12 07:16:17 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return unless (@out); # Didn't hit any conditions, must not be us.
|
2014-10-15 06:16:52 -07:00
|
|
|
|
|
|
|
return qq/Binary conversion: $out[0] ($out[2]) = $out[1] ($out[3])/,
|
|
|
|
structured_answer => {
|
|
|
|
input => [$out[0]],
|
|
|
|
operation => $out[2] . ' to ' . $out[3],
|
|
|
|
result => $out[1]};
|
2012-04-15 08:04:36 -07:00
|
|
|
};
|
2012-04-12 12:12:21 -07:00
|
|
|
|
2012-04-15 08:04:36 -07:00
|
|
|
1;
|