简体   繁体   English

如何在python中将二进制和ascii的组合转换为人类可读的格式

[英]How to convert combination of binary and ascii to human readable format in python

Below is my code for receiving data over socket. 下面是我通过套接字接收数据的代码。

from twisted.internet.protocol import Factory
from twisted.protocols.basic import LineReceiver
from twisted.internet import reactor

class Chat(LineReceiver):

    def lineReceived(self, line):
        print(line)

class ChatFactory(Factory):

   def __init__(self):
       self.users = {} # maps user names to Chat instances

   def buildProtocol(self, addr):
       return Chat()

reactor.listenTCP(9600,ChatFactory())
reactor.run()

I got response from the client with 我得到了客户的回应

b'$$\x00pP$\x91\x97\x01\xff\xff\x99\x99P000002.000,V,0000.0000,N,00000.0000,E,0.00,000.00,060180,,*09|||0000|0000,0000|000000113|00000[\x86'

which is combination of hex code and ascii, the location information is in ascii format. 它是十六进制代码和ascii的组合,位置信息采用ascii格式。 What is the best way to convert this data into human readable format? 将这些数据转换为可读格式的最佳方法是什么?

I need to parse header, L and ID . 我需要解析标头L和ID

<$$><L><ID><command><data><checksum><\r\n>
  • header = 2 bytes and it will be in hex code representation of 0x24 标头= 2个字节,它将以0x24十六进制代码表示
  • L = 2 bytes and it is in the format of hex code . L = 2个字节,采用十六进制代码格式。
  • ID = 7 bytes and it is in the format of hex code . ID = 7字节,采用十六进制代码格式。
  • command = 2 bytes, it will be in hex code 命令= 2个字节,将以十六进制代码表示
  • data will be in ascii format. 数据将为ascii格式。
  • checksum = 2 bytes and in hex code 校验和= 2字节,十六进制代码

Thanks in advance. 提前致谢。

This can be solved just with the binary string: 只需使用二进制字符串即可解决:

import struct
header = line[:2]
if header!=b'$$':
    raise RuntimeError('Wrong header')
# Assumes you want two have 2 bytes, not one word
L = struct.unpack('BB',line[2:4])
ID = struct.unpack('7B', line[4:11])
location = line[11:]
print 'L={},{}, ID={}, location={}'.format(L[1],L[2], ''.join(str(b) for b in ID, location)

The link to struct is in the other answer struct的链接在另一个答案中

You can use struct.unpack() , but you have to know what you are gonna unpack (int, long, signed, etc.), the endiness if it is encoded on more that 1 bytes, ASCII are 7 bits, encoded on 1 byte. 您可以使用struct.unpack() ,但是您必须知道要解压缩的内容(int,long,signed等),如果编码超过1个字节,ASCII编码为7位,编码为1个,则表示结束符字节。

Read more about struct here . 在此处阅读有关struct的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将 json 转换为 python 中的人类可读格式? - how to convert json into human readable format in python? 如何使用 Python 将 FlatBuffer 消息从二进制转换为人类可读的文本格式? - How to convert FlatBuffer message from binary to human readable text format with Python? 如何使用 python 将原始 CAN 数据转换为人类可读格式? - How to convert raw CAN data to human readable format using python? 如何使用 Python 将二进制日期时间从 SQLServer 转换为人类可读的日期和时间 - How to convert binary datetime from SQLServer to human readable date and time with Python 如何将长格式日期转换为人类可读格式 - How to convert a long format Date to Human Readable format 无法将Epoch值转换为人类可读日期格式python - Unable to convert Epoch value to Human readable date format python 将python中的单位转换为非人类可读格式 - Convert number with units in python to NON human readable format 如何在Python中将RFC 822时间戳转换为人类可读格式? - How can I convert an RFC 822 timestamp into a human readable format in Python? 将整数日期格式转换为人类可读格式 - Convert integer date format to human readable format 如何将 Python 二进制转换为 ASCII - How to convert Python binary to ASCII
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM