Python 转换中文成对应的unicode

 

有个单片机项目,需要支持中文,含有unicode字库,但需要按照下边的格式定义, 一个个转换太麻烦 ,尝试些一个python脚本做这个事情,记录一下

#coding=utf-8


# 使用说明:
# 读取Input.c 文件中如下格式的内容并将""间的内容转换成对应的unicode#define
# 例如: #define   _sC_V2xx_Host     "主机模式"   输出:  #define _sC_V2xx_Host    "\x4e\x3b\x67\x3a\x6a\x21\x5f\x0f"    //"主机模式"
#

import os
import sys
import binascii

def Unicode2HexStr(Unicde_Str):
 
    Hex_Str = ""
 
    for i in range(0, len(Unicde_Str)):
 
        Hex_Str += (hex(ord(Unicde_Str[i])).replace('0x','').zfill(4)) + ";"
        #print (hex(ord(Unicde_Str[i])).replace('0x','').zfill(4)) + ";"
    return Hex_Str
 
 
out = open('Output.c','w')
f = open('Input.c','r')
for line in f.readlines():
    print line.strip()
    StringInFile = line.strip()       # remove space at header and tail
    if StringInFile.find('define') ==1 :  
        StrItem = StringInFile
        StringInFile = StringInFile.split('"')
        StrItem = StrItem.split()
        #print StrItem
        #print "-----------------------"
        #print StringInFile[1]

        s = unicode(StringInFile[1], "utf-8")
        

        Str = Unicode2HexStr(s)
        print Str

        Str = Str.replace("'", "", 100)

        Str2 = Str.split(';')
        print Str2

        OutStr = ''
        print len(Str2)
        for i in range(0,len(Str2)-1):
            #print int("".join(Str2[i]), 16)
            #HexNum=HexNum+Str2[i]
            OutStr = OutStr + Str2[i]

        Str3 = OutStr.split(' ')
        OutStr = OutStr.replace("0x", "", 10);
        out.write(StrItem[0])
        out.write(' ')
        out.write(StrItem[1])
        out.write('    ')
        out.write('"')
        for i in  range(0,len(OutStr)):
            if i%2 :
                pass
            else:
                out.write(r'\x')
            out.write(OutStr[i])
        out.write('"')
        out.write('    //')
        out.write(StringInFile[1])
        out.write('\n')
f.close()
out.close()

 

         

展开阅读全文

没有更多推荐了,返回首页