将字符串转换为HEX字符

时间:2016-05-03 14:24:42

标签: string python-2.7 hex

我有以下简短代码:

import socket
from sys import *

host = "10.10.10.10"
port = 7142
buf  = 1024

tcpSock = socket.socket()
tcpSock.settimeout(100)
tcpSock.connect((host,port))

## Send message
data ='\x01\x30\x41\x30\x41\x30\x36\x02\x30\x31\x44\x36\x03\x74\x0d'
if(tcpSock.send(data)):    
    print "Sending message:",data
data = tcpSock.recv(4096) 
tcpSock.close()

print "Received message:", data
#print "Received message:", data.strip().decode("hex")
输出是:

发送讯息:☺0A0A06☻01D6♥t

收到留言:☺00AB12☻0200D60000040001♥t

我被困在哪里是如何将“收到的消息”从服务器解码回HEX字符

由于 亚历

1 个答案:

答案 0 :(得分:5)

请尝试此代码

import binascii
mytext='☺00AB12☻0200D60000040001♥t'
print binascii.hexlify(mytext)

我收到了这个输出

3f3030414231323f303230304436303030303034303030313f74