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:

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
    Thanks for the reply
    , Please let me know what I should read to get familiar with these ( I do not have much computer background ).
    when i print the java output (each element of the array) I get the following result ..

    1 35 69 103 -119 -85 -51 -17 1 35 69 103 -119 -85 -51 -17


    The perl one gives this output

    1 35 69 103 137 171 205 239 1 35 69 103 137 171 205 239

      Apparently, bytes as considered signed in Java. No problem.

      I could fix the map, but it's getting quite long. It's easier to just use pack and unpack.

      my $key = "0123456789ABCDEF0123456789ABCDEF"; local $_ = $key; s/^0+//; $_ = "0$_" if length % 2 == 1; $_ = "00$_" if /^[89ABCDEFabcdef]/; my @b = unpack('c*', pack('H*', $_));
      Could you do me a favour and test to make sure it works for "FFFF", "000102" and "102"?
        It works for 000102 and 102 but it dosent work for FFFF

        FFFF in java 0 -1 -1 the Perl one 15 -1 -16