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

Hi all,

I am trying converting java code to perl code.

-------------------------
BigInteger(byte[] val)
-------------------------
BigInteger
This is Java BigInteger constructor.

Perl Math::BigInt doesn't have a same constructor.
But I should make it and the same Java BigInteger constructor.
How can i do following two code same output.
Is it simple? or very difficult?
I have no idea..

Java
--------------------------------------------
import java.security.MessageDigest;
import java.math.BigInteger;

public class Hash
{
public static void main( String[] args ) throws Exception
{
MessageDigest md5 = MessageDigest.getInstance("MD5");
String plain = "abcd1234";
BigInteger digest = new BigInteger(md5.digest(plain.getBytes("UTF-8")));

System.out.println( digest.abs() );
}
}
--------------------------------------------
Perl
--------------------------------------------
use Digest::MD5 'md5_hex';
use Math::BigInt;

my $plain = "abcd1234";
my $digest = Math::BigInt::->from_hex(md5_hex $plain);
print $digest, "\n";
--------------------------------------------

Replies are listed 'Best First'.
Re: Converting Java to Perl (BigInteger)
by tobyink (Canon) on Feb 28, 2013 at 12:31 UTC

    For those new to this discussion, the above post is a follow-on from Converting Java to Perl (MD5). The question can be summed up as: how do I treat a hexadecimal string as a big two's complement integer?

    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name
Re: Converting Java to Perl (BigInteger)
by tmharish (Friar) on Feb 28, 2013 at 13:25 UTC

    Handle the sign and you should be fine: ( wow that rhymes! )

    use strict ; use warnings ; use Math::BigInt ; my $hex = '-a' ; my $sign = '0' ; if( $hex =~ /^-/ ) { $hex =~ s/^-//g ; $sign = 1 ; } my $big_int = Math::BigInt->new( '0x'.$hex ) ; my $binary = $big_int->as_bin() ; my $two_comp = $binary ; $two_comp =~ s/^0b/$sign/; print "$two_comp\n";