关于2312查表可以看这篇内容,它进行了编码工作为了解决他的模块GB2312问题,首先贴出GB2312编码查表的引用地址
# update=1
# 上面1表示更新,0表示不更新
ver=1.5
#####
import machine,ssd1306,time
from machine import Pin
oled_vcc=Pin(27,Pin.OUT,value=1)
oled_gnd=Pin(26,Pin.OUT,value=0)
try:
time.sleep(0.7)
i2c=machine.SoftI2C(scl=machine.Pin(14),sda=machine.Pin(12)) #这里是引脚改成自己的,不能偷懒
oled=ssd1306.SSD1306_I2C(128,64,i2c)
oled.fill(0)
oled.show()
except:
print('OLED 屏幕故障')
class gb2312(object):
def __init__(self):
self.f = open('utf2gb2312.bin', 'r', encoding='utf-8')
def b2i(self, byte): # bytes转int
r = 0
for i in range(len(byte)):
r = (r << 8) + byte[i]
return r
def i2b(self, num): # int转bytes
num = int(num, 16)
return num.to_bytes(2, 'big')
def one_char(self, char): # 将一个字符转化成gb2312
utf_byte = char.encode('utf-8')
r = self.B_S(0, 7296, self.b2i(utf_byte))
gb2312_byte = self.i2b(r)
# print(gb2312_byte)
return gb2312_byte
def strs(self, st): # 将字符串转化成gb2312
r = b''
for s in st:
# print(s.encode('utf-8'))
if len(s.encode('utf-8')) <= 1:
r += s.encode('utf-8')
else:
r += self.one_char(s)
return r
def B_S(self, low, high, m): # 二分查找
if 0 <= low <= high <= 7296:
mid = (low + high) // 2
self.f.seek(mid * 12)
data = self.f.read(12)
utf = data[0:6]
if int(utf, 16) < m:
return self.B_S(mid + 1, high, m)
elif int(utf, 16) > m:
return self.B_S(low, mid - 1, m)
else:
return data[7:-1]
def get_gb2312(self,a):
f = open('HZK16S','rb')
a=self.strs(a)
#a = a.encode('gb2312')
area = a[0]-160
index = a[1]-160
offset = (94*(area-1)+(index-1))*32
try:
f.seek(offset)
btxt=f.read(32)
except:
print("out range chinese")
btxt=None
f.close()
return btxt
def __del__(self):
self.f.close()
def chchar(oled,x,y,btxt,origin=1,color = 1):
# if ((x+16)>= self.width) or ((y+16)>= self.height):
# return
for i in range(16):
t1 = btxt[i<<1]
t1 = int(bin(t1)[2:])
t1 = "%08d" % t1
t2 = btxt[(i<<1)+1]
t2 = int(bin(t2)[2:])
t2 = "%08d" % t2
t = t1+t2
for j in range(16):
if t[j]=='1':
oled.pixel((x+j),(y+i),color)
oled.show()
gb=gb2312()
chchar(oled,0,0,gb.get_gb2312('行') )
库表1
库表2