perl-unicode

Re: MD5 digest of UTF-8 string in Perl 5.8

2002-10-23 08:30:09
How can I calculate the MD5 message digest of a Unicode string in Perl
5.8? The MD5 hash algorithm naturally expects a sequence of bytes as its
input, and I have a string with a sequence of characters. I tried

  $ perl -e 'use Digest::MD5 qw(md5_hex); print md5_hex("\x{20ac}");'
  Wide character in subroutine entry at -e line 1.

I'd do something like that:

  use Encode;
  use Digest::MD5 qw(md5_hex);

  sub md5_hex
  {
    my $string = shift;
    Encode::_utf8_off ($string);
    return md5_hex ($string);
  }

In this case setting the UTF8 flag off is OK because $string is a copy
of the original string which won't be used outside of the subroutine.
You have to be careful when playing with Encode::is_utf8()
Encode::_utf8_on() and Encode::_utf8_off() but it's very handy.

Best regards,
-- 
IT'S TIME FOR A DIFFERENT KIND OF WEB
================================================================
  Jean-Michel Hiver - Software Director
  jhiver(_at_)mkdoc(_dot_)com
  +44 (0)114 255 8097
================================================================
                                      VISIT HTTP://WWW.MKDOC.COM

<Prev in Thread] Current Thread [Next in Thread>