import re
import sys
if len(sys.argv) < 2:
print("请传入要打开的文件名作为参数")
else:
file_name = sys.argv[1]
# 读取文件内容
with open(file_name, 'r', encoding='utf-8') as file1:
content = file1.read()
with open('output.txt', 'w') as file2:
pattern = r'&#x([\da-fA-F]{4});'
matches = re.findall(pattern, content)
lines = content.split('\n')
i = 0
for line in lines:
if re.match(r'^[a-zA-Z]', line.strip()):
print("#define LV_", end='')
print(line.strip().upper(), end='')
file2.write(line.strip().upper())
unicode_char = chr(int(matches[i], 16))
utf8_bytes = unicode_char.encode('utf-8')
# 将 UTF-8 编码转换为十六进制表示
utf8_hex = ' '.join(['{:02X}'.format(b) for b in utf8_bytes])
file2.write(" \"")
print(" \"", end='')
for j in range(0, len(utf8_hex), 3):
dat1 = utf8_hex[j:j + 2]
if len(dat1) == 2: # 确保取出的单位长度为2
file2.write("\\x")
print("\\x", end='')
file2.write(dat1)
print(dat1, end='')
file2.write("\"\n")
print("\"")
i += 1
unicon转utf-8脚本
最新推荐文章于 2024-05-06 17:20:22 发布