use strict; package ConvUTF8; my(%sSjis, %sJis); BEGIN { my($sSJIS, $sJIS, $sUni); open(IN, "<./jis0208.txt"); while() { next if(/^#/); ($sSJIS, $sJIS, $sUni) = split; $sSjis{pack("H4", substr($sUni, 2))} = pack("H4", substr($sSJIS, 2)); } close(IN); } sub ::Utf8toSJIS($) { my ($uStr) = @_; my($iPos, $sKey, $iLen); my($sRes); $iPos = 0; $sRes = ""; for($iPos = 0;$uStr ne ""; $uStr = substr($uStr, $iLen)) { if ($uStr =~ /^([\x00-\x7F])/) { $iLen = 1; $sRes .= $1; } elsif ($uStr =~ /^([\xC0-\xDF])([\x80-\xBF])/) { $iLen = 2; $sRes .= pack("n",((ord($1) & 31) << 6) | (ord($2) & 63)); #または (下記注を参照) #$sKey = pack("n",((ord($1) & 31) << 6) | (ord($2) & 63)); #$sRes .= $sSjis{$sKey};   } elsif ($uStr =~ /^([\xE0-\xEF])([\x80-\xBF])([\x80-\xBF])/) { $iLen = 3; $sKey = pack("n",((ord($1) & 15) << 12)| ((ord($2) & 63) << 6) | (ord($3 ) & 63)); $sRes .= $sSjis{$sKey}; } else { die "Whoah! Bad UTF-8 data!$uStr\n"; } } return $sRes; }