这是我的php代码:
$str = '你好';
$input_encoding = mb_detect_encoding($str, array('ASCII','GB2312','GBK','UTF-8'), true);
echo sprintf('input encoding:%s', $input_encoding);
$str_gb = iconv($input_encoding, 'GBK', true);
echo sprintf("utf8 encoding:%s\n", $str);
echo sprintf("gb encoding md5:%s\n", md5($str_gb));
echo sprintf("utf8 encoding md5:%s\n", md5($str));
这是我的python代码:</p>
#!/usr/bin/env python
#coding:utf-8
import urllib
import hashlib
str_u = u'你好'
str_gb = str_u.encode('gbk')
str_u8 = str_u.encode('utf-8')
m = hashlib.md5()
m.update(str_gb)
str_gb_md5 = m.hexdigest()
m.update(str_u8)
str_u8_md5 = m.hexdigest()
print 'gb md5:%s' % str_gb_md5
print 'utf-8 md5:%s' % str_u8_md5
PHP代码结果是:
input encoding:CP936
utf8 encoding:你好
gb encoding md5:c4ca4238a0b923820dcc509a6f75849b
utf8 encoding md5:7eca689f0d3389d9dea66ae112e5cfd7
python代码结果是:
gb md5:b94ae3c6d892b29cf48d9bea819b27b9
utf-8 md5:a8a343223373c7d78c3fb8bad2d786c3
这是我的编程环境:PHP 5.5.4 (cli)&Python 2.6.8
谢谢!