Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi,
What is the Perl equivalent of the java code , I think I should be using unpack but am not sure .
In java String keyString = "0123456789ABCDEF0123456789ABCDEF"; byte key[] = new BigInteger(keyString, 16).toByteArray();

Thanks .

Replies are listed 'Best First'.
Re: Perl equivalent of Java code
by jdporter (Paladin) on Jan 19, 2006 at 16:47 UTC

    You can use unpack. But unless you're already a pack/unpack wizard, it will take you about 10 times longer to figure out the magical unpack formula than, for example, the following simple solution:

    my $key = "0123456789ABCDEF0123456789ABCDEF"; my @b = map { chr hex $_ } $key =~ /(..)/g;
    We're building the house of the future together.

      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).

        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

Re: Perl equivalent of Java code
by davido (Cardinal) on Jan 19, 2006 at 16:47 UTC

    Could you explain for those who don't know how to read Java what it does? A description of the actual problem would enable more people here to respond with better accuracy.


    Dave

      Thanks for your reply
      I am not very familiar with Java but here goes
      From the javadoc

      BigInteger(String val, int radix) Translates the String representation of a BigInteger in the specified radix into a BigInteger.


      toByteArray() Returns a byte array containing the two's-complement representation of this BigInteger.
Re: Perl equivalent of Java code
by radiantmatrix (Parson) on Jan 20, 2006 at 17:44 UTC

    I don't know about the toByteArray() method, but the conversion to a BigInteger-like object could be handled by Math::BigInt. You could then use standard algorithms for dividing an integer into bytes, I suppose.

    <-radiant.matrix->
    A collection of thoughts and links from the minds of geeks
    The Code that can be seen is not the true Code
    I haven't found a problem yet that can't be solved by a well-placed trebuchet