python输出GB2312和GBK码表

# -*- coding: utf-8 -*-
# 94个区,每个区94个汉字/符号/python会throw exception的东东
# 内码 := <区码+0xa0> <位码+0xa0>, little endian. 0xa0+94=0xfe
# 汉字从1601(啊)开始,它的内码是B0 A1. 鳋的区位码是8794, 内码是F7 FE
import sys
qv = int(sys.argv[1]) if len(sys.argv) == 2 else 0
if qv < 1 or qv > 94: qv = 1
def pr(*args, **kwargs): kwargs['end'] = ''; print(*args, **kwargs)
s = ['','','','','','','','','','']
pr('   ')
for i in range(10): pr(s[i],'')
print()
for y in range(10):
    pr(s[y],'')
    for x in range(10):
        if y == 9 and x > 4: break
        if y == 0 and x == 0: pr('   ')
        else: 
            wei = y * 10 + x
            code = ((0xa0 + qv) << 8) | (0xa0 + wei)
            try: pr((code).to_bytes(2, 'big').decode('gb2312'), '')
            except Exception: pr('   ')
    print()
# 折腾了半天,写不动程序了,唉……
View Code
import sys
def pr(*args, **kwargs): kwargs['end'] = ''; print(*args, **kwargs)
def head():
    pr('    ')
    for i in range(16): pr('%2X ' % i)
    print()
for y in range(0x8140, 0xfefe, 16):
    t = y & 0xff
    if t < 0x40: continue
    elif t == 0x40: head()
    pr(hex(y)[2:5].upper(),'')
    for x in range(16):
        try: pr((y + x).to_bytes(2, 'big').decode('gbk'), '')
        except Exception: pr('  ')
    print()
View Code

posted @ 2021-12-16 10:40  Fun_with_Words  阅读(236)  评论(0编辑  收藏  举报









 张牌。