简体   繁体   English

我如何将像 b'\\x08\\x00\\x00\\x00' 这样的 Python/socket 读取的雷达数据解码为数字

[英]How do I decode radar data read by Python/socket like b'\x08\x00\x00\x00' to numbers

I'm developing a Python3 program to process data from radar.我正在开发一个 Python3 程序来处理来自雷达的数据。 In general, radar send data(hex numbers) to a port of my computer and I use socket to listen that port.通常,雷达将数据(十六进制数)发送到我计算机的端口,我使用套接字来侦听该端口。 And use code below to recive 4 bytes of data.并使用下面的代码接收 4 个字节的数据。

data = connection.recv(4)
print(data)

When testing my programm, radar send 08 00 00 01 and program print b'\\x08\\x00\\x00\\x01' .在测试我的程序时,雷达发送08 00 00 01并程序打印b'\\x08\\x00\\x00\\x01' I understand the '\\x' means that the next to characters is a hex number, but I want to get numbers like [08, 00, 00, 01] or a normal string from it.我知道 '\\x' 意味着字符的旁边是一个十六进制数,但我想从中得到像[08, 00, 00, 01]或一个普通的字符串。 Tried the most obvious way:尝试了最明显的方法:

strData = data.decode()
print(strData.split('\x'))

Failed with SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \\xXX escape .SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \\xXX escape失败SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: truncated \\xXX escape

And situation goes worse if radar send 08 0D 00 00 , print(data) will print b'\\x08\\r\\x00\\x00' , which makes me realized that string operations can not handle this problem.如果雷达发送08 0D 00 00 ,情况会更糟, print(data)将打印b'\\x08\\r\\x00\\x00' ,这让我意识到字符串操作无法解决这个问题。

So, what is the right way to convert bytes like b'\\x08\\x00\\x00\\x01' to numbers like 08 00 00 01 .那么,将像b'\\x08\\x00\\x00\\x01'这样的字节转换为像08 00 00 01这样的数字的正确方法是什么。

String encoding drives me carzy -.- Although I am using Python3, solutions in Python2.7 is OK as well.字符串编码让我抓狂-.- 虽然我使用的是 Python3,但 Python2.7 中的解决方案也可以。 Thanks in advance.提前致谢。

I guess you already get the binary data.我猜你已经得到了二进制数据。 If you really want the hex-representation as a string, this Answer to similar question could help you.如果你真的想要十六进制表示作为一个字符串,这个对类似问题的回答可以帮助你。

If you would like to interpreted the data as for example a float, struct could be used如果您想将数据解释为例如浮点数,则可以使用结构

import struct
x = struct.unpack('f',data)

For this specific case - you have a string of bytes, and you want to convert each byte to its integer equivalent - you can convert the bytestring directly to a list.对于这种特定情况 - 您有一个字节字符串,并且您希望将每个字节转换为其等效的整数 - 您可以将字节字符串直接转换为列表。

>>> bs = b'\x08\x00\x00\x01'
>>> ints = list(bs)
>>> ints
[8, 0, 0, 1]

This works because这有效,因为

bytes objects actually behave like immutable sequences of integer bytes 对象实际上表现得像不可变的整数序列

so所以

[For] a bytes object b, b[0] will be an integer, while b[0:1] will be a bytes object of length 1. (This contrasts with text strings, where both indexing and slicing will produce a string of length 1) [对于]一个字节对象 b,b[0] 将是一个整数,而 b[0:1] 将是一个长度为 1 的字节对象。(这与文本字符串形成对比,其中索引和切片都会产生一个字符串长度 1)

(Quotations from the docs for the bytes type). (从bytes类型的文档引用)。

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

相关问题 how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python - how to store bytes like b'PK\x03\x04\x14\x00\x08\x08\x08\x009bwR\x00\x00\x00\x00\x00\x00\x00 to dataframe or csv in python python 视频 b'\x1aE\xdf\xa3\x01\x00\x00\x00\x00\x00\x00\ - python video b'\x1aE\xdf\xa3\x01\x00\x00\x00\x00\x00\x00\ 如何删除那些“\x00\x00” - How to remove those "\x00\x00" 我如何在 python 中将 "\\xff\\xfet\\x00e\\x00s\\x00t\\x00" 转换为 b'\\xff\\xfet\\x00e\\x00s\\x00t\\x00"' - How can i convert "\xff\xfet\x00e\x00s\x00t\x00" into b'\xff\xfet\x00e\x00s\x00t\x00"' in python 如何在python中用\\x00填充地址 - How to pad an address with \x00 in python 这个'\\ x00 \\ x00 \\ x00 \\ x05'的编码是什么? - What encoding is this '\x00\x00\x00\x05'? Pyparsing 将解析后的 \\x00 转换为 \\\\x00 - Pyparsing transforms parsed \x00 into \\x00 BIT 0表示为'\\ x00' - BIT 0 represented as '\x00' 用空格转换\\ x00 - Converting \x00 with a space 来自python程序的配置单元查询返回输出,如“ x00e \\ x00” \\ x00” - Hive query from python program returns output like “x00e\x00”\x00"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM