in reply to Re^2: Bit by a Hex String
in thread Bit by a Hex String
#!/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
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Re: Bit by a Hex String
by abitkin (Monk) on Aug 29, 2003 at 15:29 UTC | |
by Abigail-II (Bishop) on Aug 29, 2003 at 15:45 UTC | |
by abitkin (Monk) on Aug 29, 2003 at 15:52 UTC |