# 输入汉字,输出一段arduino代码,代码能将这些汉字转换为GBK编码,以便在arduino ide中驱动只兼容GBK编码的设备
import sys
import os
# 读取汉字
def readChinese():
print("请输入汉字,以回车结束:")
chinese = input()
return chinese
# 将汉字转换为GBK编码
def toGBK(chinese):
gbk = chinese.encode('gbk')
return gbk
# 将GBK编码转换为十六进制
def toHex(gbk):
hex = gbk.hex()
return hex
# 将十六进制转换为arduino代码
def toArduinoCode(hex):
arduinoCode = ""
for i in range(0, len(hex), 2):
arduinoCode += "Serial.write(0x" + hex[i:i+2] + ");\n"
return arduinoCode
# 将arduino代码写入文件
def writeToFile(arduinoCode):
file = open("arduinoCode.txt", "w")
file.write(arduinoCode)
file.close()
if __name__ == "__main__":
chinese = readChinese()
gbk = toGBK(chinese)
hex = toHex(gbk)
arduinoCode = toArduinoCode(hex)
writeToFile(arduinoCode)
print("已将arduino代码写入文件arduinoCode.txt")
os.system("pause")
最后编辑:2023年12月28日
©著作权归作者所有