python随机生成汉字并查看编码方式

# -*- coding: utf-8 -*-
import random
import chardet

"""用于随机生成汉字"""

status = 0
for i in range(1, 15):
    str2 = ""
    try:
        for j in range(0, 83):
            head = random.randint(0xB0, 0xCF)
            body = random.randint(0xA, 0xF)
            tail = random.randint(0, 0xF)
            val = (head << 8) | (body << 4) | tail
            str1 = "%x" % val
            str2 += str1.decode('hex').decode('gb2312').encode("utf-8")
            print str2
    except:
        status = 1

    print "status :   " + str(status)
    if status == 0:
        break
print chardet.detect(str2)

你可能感兴趣的:(python随机生成汉字并查看编码方式)