Here's a Math::BigInt solution.
#!/usr/bin/perl
use strict;
use warnings;
use Math::BigInt;
$_ = "DEADBEEFBABEFACE1234567890";
my $num = Math::BigInt -> new (0);
# Convert the number to a Math::Int decimal number.
while (length) {
my $chunk = substr $_, 0, 4, "";
$num *= 2 ** (4 * length $chunk);
$num += hex $chunk;
}
# Multiply by 2, add 1.
$num *= 2;
$num += 1;
# Convert the Math::BigInt number to hex.
my @a;
while ($num) {
push @a => sprintf "%X" => $num % 16;
$num /= 16;
}
my $new = join "" => @a;
print $new, "\n";
And here's an Inline::BC solution:
#!/usr/bin/perl
use strict;
use warnings;
use Inline 'BC';
print x("DEADBEEFBABEFACE1234567890");
__DATA__
__BC__
ibase = 16
obase = 10
define x (a) {
return (a * 2 + 1)
}
Abigail
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.
|