in reply to Re: Perl equivalent of Java code
in thread Perl equivalent of Java code
Not quite. There are three bugs in your code:
Incorrect results are returned when the most significant bit is set. A sign bit should be added, adding an extra byte. *
"FFFF" should return ("\x00", "\xFF", "\xFF").
Incorrect results are returned when there are leading zeroes.
"000102" should return ("\x01", "\x02").
Incorrect results are returned when an odd number of hex digits are provided.
"102" should return ("\x01", "\x02").
Relevant docs:
java.Math.BigInteger.BigInteger(java.lang.String, int)
java.Math.BigInteger.toByteArray()
java.Math.BigInteger.bitLength()
Fixed code:
my $key = "0123456789ABCDEF0123456789ABCDEF"; local $_ = $key; s/^0+//; $_ = "0$_" if length % 2 == 1; $_ = "00$_" if /^[89ABCDEFabcdef]/; my @b = map { chr hex $_ } /(..)/g;
* – I'm assuming new BigInteger("FFFF", 16) is considered to be 65535 (not -1).
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^3: Perl equivalent of Java code
by Anonymous Monk on Jan 19, 2006 at 17:31 UTC | |
by ikegami (Patriarch) on Jan 19, 2006 at 17:40 UTC | |
by ikegami (Patriarch) on Jan 19, 2006 at 17:48 UTC | |
by Anonymous Monk on Jan 20, 2006 at 03:46 UTC | |
by ikegami (Patriarch) on Jan 20, 2006 at 05:22 UTC |