#!/usr/bin/perl use strict; use warnings; use Encode; use utf8; binmode STDOUT, ":utf8"; my $t = "これは This is ペン a pen デスね。東京タワーに行きましたYO"; my $hv = str2hv($t); print "$t\n"; printf "%08X\n", $hv; sub str2hv { my ($t) = @_; $t = Encode::encode_utf8($t) if utf8::is_utf8($t); my $hv = 0; my $hash_size = 4294967291; foreach my $c (split(//, $t)) { my $n = unpack("C", $c); $hv = ($hv * 256 + $n) % $hash_size; } return $hv; }
% ./hash-function.pl これは This is ペン a pen デスね。東京タワーに行きましたYO 4C2F3B05