Python2.7:字符转UFT-8、GBK、BIG5并得到bytes

Python2.7:字符转UFT-8、GBK、BIG5并得到bytes

# encoding: utf-8

def hexstr(s):
    return ''.join([hex(ord(c)).replace('0x','\\x') for c in s])

# 转big5
def toBig5(s):
    s1 = s.decode('utf-8')  
    lis = []
    for e in list(s1):
        try:
            lis.append(e.encode('big5'))
        except:
            lis.append('&#%d;' % ord(e))
    return hexstr(''.join(lis))

# 转utf-8
def toUtf8(s):
    s1 = s.decode('utf-8')  
    lis = []
    for e in list(s1):
        lis.append(e.encode('utf-8'))
    return hexstr(''.join(lis))

# 转gbk
def toGbk(s):
    s1 = s.decode('utf-8')  
    lis = []
    for e in list(s1):
        lis.append(e.encode('gbk'))
    return hexstr(''.join(lis))

# 调用入口
if __name__ == '__main__':
    toGbk("用户登陆")
    toUtf8("用户登陆")
    toBig5("用户登陆")

#用户登陆
#utf8: \xe7\x94\xa8\xe6\x88\xb7\xe7\x99\xbb\xe9\x99\x86
#gbk: \xd3\xc3\xbb\xa7\xb5\xc7\xc2\xbd
#big5:\xa5\xce\x26\x23\x32\x35\x31\x34\x33\x3b\xb5\x6e\x26\x23\x33\x38\x34\x37\x30\x3b

#word38755
#utf8:\x77\x6f\x72\x64\x33\x38\x37\x35\x35
#gbk: \x77\x6f\x72\x64\x33\x38\x37\x35\x35
#big5:\x77\x6f\x72\x64\x33\x38\x37\x35\x35

 

posted @ 2022-01-18 20:53  整合侠  阅读(318)  评论(0编辑  收藏  举报