Commit c2a364ff authored by liyuanhong's avatar liyuanhong

修改了中文转GBK的方法

parent 48049a07
#encoding:utf-8
import datetime
import random
import re
import time
from lib.protocol.Base import Base
......@@ -214,9 +215,21 @@ class MessageBase(Base):
# 字符串转换为GBK的16进制
#######################################################
def GBKString2Hex(self,data):
'''
data = str(data.encode("gbk"))
dataHex = self.str2Hex(data[2:len(data) - 1])
return dataHex
'''
dataHex = ""
strLen = len(data)
for i in range(0,strLen):
if re.search("[0-9a-zA-Z]",data[i]) != None:
dataHex = dataHex + self.str2Hex(data[i])
else:
temp = str(data[i].encode("gbk")).replace("\\x","")
dataHex = dataHex + temp[2:len(temp) - 1]
return dataHex
#######################################################
# 16进制转换为GBK字符串
......@@ -303,7 +316,7 @@ if __name__ == "__main__":
# print(MessageBase().str2Ascsii("uvwxyz"))
# print(MessageBase().int2hexStringByBytes(220400566542345564784802,20))
# print(MessageBase().str2Hex("a865h643gfdj64fd7432"))
# print(MessageBase().hex2Str("61383635683634336766646a3634666437343332"))
# print(MessageBase().hex2Str("d4c1423939383838"))
# print(MessageBase().GBKString2Hex("KZP200_V201001"))
# print(MessageBase().hex2GBKString("4b5a503230305f56323031303031"))
# print(MessageBase().str2Hex("\xd3\xe5B23CX"))
......@@ -312,8 +325,8 @@ if __name__ == "__main__":
# print(MessageBase().getCheckCode("8001000501314620111800000000000200"))
# print(MessageBase().getMsgHeader())
# print(MessageBase().generateMsg())
# print(MessageBase().GBKString2Hex("渝B23CX"))
# print(MessageBase().hex2GBKString("5c7864335c7865354232334358"))
print(MessageBase().GBKString2Hex("渝B23CX"))
print(MessageBase().hex2GBKString("d3e54232334358"))
# print(MessageBase().int2BCD(123456789012345,10))
# print(MessageBase().getRandomNum(3000,5000,[2,4,6,8,10,12],4))
# print(MessageBase().getRandomStr(10))
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment