use Math::BigInt b/c sprintf can't handle >64b
parent
b98409231b
commit
f82ad596e5
|
@ -2,7 +2,7 @@ package DDG::Goodie::HexToDec;
|
|||
# ABSTRACT: Convert hexidecimal to decimal
|
||||
|
||||
use DDG::Goodie;
|
||||
use bignum;
|
||||
use Math::BigInt;
|
||||
|
||||
triggers query_raw => qr/\b0x[0-9a-fA-F]+\b/;
|
||||
|
||||
|
@ -22,27 +22,25 @@ attribution cpan => 'majuscule',
|
|||
my $css = share("style.css")->slurp;
|
||||
|
||||
sub wrap_html {
|
||||
my ($decimal, $octal, $bin) = @_;
|
||||
my ($decimal, $octal) = @_;
|
||||
return "<style type='text/css'>$css</style>" .
|
||||
"<div class='zci--conversions text--primary'>" .
|
||||
"$decimal <sub>10</sub>" .
|
||||
"<div style='font-size:75%'>" .
|
||||
"<span class='text--secondary'>Binary: </span> $bin " .
|
||||
"<div class='zci--hextodec text--primary'>" .
|
||||
"<div class='hextodec--decimal'>" .
|
||||
"<span class='text--secondary'>Decimal:</span> $decimal" .
|
||||
"</div>" .
|
||||
"<div>" .
|
||||
"<span class='text--secondary'>Octal: </span> $octal" .
|
||||
"</div>";
|
||||
"</div>" .
|
||||
"</div>";
|
||||
}
|
||||
|
||||
handle query_raw => sub {
|
||||
m/\b0x([0-9a-fA-F]+)\b/;
|
||||
my $hex = $1;
|
||||
my $decimal = sprintf "%lu", hex $hex;
|
||||
my $octal = sprintf "%lo", hex $hex;
|
||||
my $bin = sprintf "%b", hex $hex;
|
||||
return "$hex base 16 = $decimal base 10 == " .
|
||||
"$octal base 8 == " .
|
||||
"$bin base 2",
|
||||
html => wrap_html($decimal, $octal, $bin);
|
||||
my $decimal = Math::BigInt->from_hex($hex);
|
||||
my $octal = $decimal->as_oct;
|
||||
return "$hex base 16 = $decimal base 10 = $octal base 8",
|
||||
html => wrap_html($decimal, $octal);
|
||||
};
|
||||
|
||||
0x41414141;
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
.zci--answer .zci--conversions {
|
||||
font-size: 1.5em;
|
||||
.zci--answer .zci--hextodec {
|
||||
font-weight: 300;
|
||||
padding-top: .25em;
|
||||
padding-bottom: .25em;
|
||||
}
|
||||
word-break: break-word;
|
||||
}
|
||||
.zci--answer .zci--hextodec .hextodec--decimal {
|
||||
font-size: 1.7em;
|
||||
}
|
||||
|
|
37
t/HexToDec.t
37
t/HexToDec.t
|
@ -11,21 +11,34 @@ zci is_cached => 1;
|
|||
ddg_goodie_test(
|
||||
[qw( DDG::Goodie::HexToDec )],
|
||||
'0xd1038d2e07b42569' => test_zci(
|
||||
'd1038d2e07b42569 base 16 = 15061036807694329193 base 10 == 1504034322700755022551 base 8 == 1101000100000011100011010010111000000111101101000010010101101001 base 2',
|
||||
html => "<style type='text/css'>.zci--answer .zci--conversions {
|
||||
font-size: 1.5em;
|
||||
'd1038d2e07b42569 base 16 = 15061036807694329193 base 10 = 01504034322700755022551 base 8',
|
||||
html => "<style type='text/css'>.zci--answer .zci--hextodec {
|
||||
font-weight: 300;
|
||||
padding-top: .25em;
|
||||
padding-bottom: .25em;
|
||||
}</style><div class='zci--conversions text--primary'>15061036807694329193 <sub>10</sub><div style='font-size:75%'><span class='text--secondary'>Binary: </span> 1101000100000011100011010010111000000111101101000010010101101001 <span class='text--secondary'>Octal: </span> 1504034322700755022551</div>"),
|
||||
word-break: break-word;
|
||||
}
|
||||
.zci--answer .zci--hextodec .hextodec--decimal {
|
||||
font-size: 1.7em;
|
||||
}
|
||||
</style><div class='zci--hextodec text--primary'><div class='hextodec--decimal'><span class='text--secondary'>Decimal:</span> 15061036807694329193</div><div><span class='text--secondary'>Octal: </span> 01504034322700755022551</div></div>"),
|
||||
'0x44696f21' => test_zci(
|
||||
'44696f21 base 16 = 1147760417 base 10 == 10432267441 base 8 == 1000100011010010110111100100001 base 2',
|
||||
html => "<style type='text/css'>.zci--answer .zci--conversions {
|
||||
font-size: 1.5em;
|
||||
'44696f21 base 16 = 1147760417 base 10 = 010432267441 base 8',
|
||||
html => "<style type='text/css'>.zci--answer .zci--hextodec {
|
||||
font-weight: 300;
|
||||
padding-top: .25em;
|
||||
padding-bottom: .25em;
|
||||
}</style><div class='zci--conversions text--primary'>1147760417 <sub>10</sub><div style='font-size:75%'><span class='text--secondary'>Binary: </span> 1000100011010010110111100100001 <span class='text--secondary'>Octal: </span> 10432267441</div>"),
|
||||
word-break: break-word;
|
||||
}
|
||||
.zci--answer .zci--hextodec .hextodec--decimal {
|
||||
font-size: 1.7em;
|
||||
}
|
||||
</style><div class='zci--hextodec text--primary'><div class='hextodec--decimal'><span class='text--secondary'>Decimal:</span> 1147760417</div><div><span class='text--secondary'>Octal: </span> 010432267441</div></div>"),
|
||||
'0xffffffffffffffffffffff' => test_zci('ffffffffffffffffffffff base 16 = 309485009821345068724781055 base 10 = 0177777777777777777777777777777 base 8',
|
||||
html => "<style type='text/css'>.zci--answer .zci--hextodec {
|
||||
font-weight: 300;
|
||||
word-break: break-word;
|
||||
}
|
||||
.zci--answer .zci--hextodec .hextodec--decimal {
|
||||
font-size: 1.7em;
|
||||
}
|
||||
</style><div class='zci--hextodec text--primary'><div class='hextodec--decimal'><span class='text--secondary'>Decimal:</span> 309485009821345068724781055</div><div><span class='text--secondary'>Octal: </span> 0177777777777777777777777777777</div></div>"),
|
||||
'0x44696f2Z' => undef,
|
||||
);
|
||||
|
||||
|
|
Loading…
Reference in New Issue