简体   繁体   English

将十六进制字符串转换为二进制字符串,用于 python 中的串行命令

[英]convert string of hex characters into a binary string for serial commands in python

I actually solved my problem but I couldn't find a clear reason why I need to jump through these hoops我实际上解决了我的问题,但我找不到明确的理由为什么我需要跳过这些箍

I am looking for a concise explanation and a better method.我正在寻找简明的解释和更好的方法。

I need to convert a string like this我需要像这样转换一个字符串

command0 = 'FF 30 30 01 51 33 34 46 EF' 

to this对此

b'\xff00\x01Q34F\xef'

which is the hex equivalent这是十六进制等价物

I put some random solutions i found together and came up with this我把我找到的一些随机解决方案放在一起并想出了这个

def convcom(data):
    return "".join([r"\x" + x for x in data.split()]).encode('utf-8').decode('unicode_escape').encode('ISO-8859-1')

Now that does produce the desired outcome, but here are some other things I tried that did not work.现在确实产生了预期的结果,但这里有一些我尝试过但没有用的其他东西。

I tried this我试过这个

def convcom(data):
    return "".join([r"\x" + x for x in data.split()]).encode('utf-8')

but I kept getting this result但我一直得到这个结果

b'\\xFF\\x30\\x30\\x01\\x51\\x33\\x34\\x46\\xEF'

I tried other solutions like hexlify but nothing worked.我尝试了其他解决方案,如 hexlify,但没有任何效果。

I also tried something to this effect我也尝试过这种效果

b"" + ""join([r"\x" + x for x in data.split()])

That just flat out failed with can't concat.那完全失败了,无法连接。

The only thing that finally worked was this唯一最终起作用的是这个

"".join([r"\x" + x for x in data.split()]).encode('utf-8').decode('unicode_escape').encode('ISO-8859-1')

you've already provide yourself a solution, but here's how i would do it你已经为自己提供了一个解决方案,但我会这样做

def str_hex_to_byte(str_hex):
    return bytes([int("0x%s" % byte, 16) for byte in command0.split()])

command0 = "FF 30 30 01 51 33 34 46 EF"
str_hex_to_byte(command0)

this will return a bytes object with b'\xff00\x01Q34F\xef'这将返回一个字节 object 和b'\xff00\x01Q34F\xef'

here's a little of explaination int() constructor takes two positional arguments first is a buffer and second its the base the first buffer will be "0xFF" which is a string that contains the decimal number 0xFF which is 255 in base 16 so that's why i send as second parameter 16 now the results its gonna be a list of int with base 16这里有一些解释int()构造函数采用两个位置 arguments 第一个是缓冲区,第二个是它的基数,第一个缓冲区将是“0xFF”,这是一个包含十进制数 0xFF 的字符串,它在基数 16 中为 255,所以这就是为什么我现在作为第二个参数 16 发送结果将是一个以 16 为底数的 int 列表

[0xFF, 0x30, 0x30, 0x01, 0x51, 0x33, 0x34, 0x46, 0xEF,]

now we pass it to bytes() constructor that could receive a list of int with members inside the range 0-255 (basically UINT8) and that will be the return of our function现在我们将它传递给bytes()构造函数,它可以接收一个 int 列表,其成员在 0-255 范围内(基本上是 UINT8),这将是我们 function 的返回值

hope this was useful, i didn't explain str.split() method because you already use it so not need to do that希望这有用,我没有解释str.split()方法,因为您已经在使用它,所以不需要这样做

def str_hex_to_byte(str_hex): return bytes([int("0x%s" % byte, 16) for byte in command0.split()]) def str_hex_to_byte(str_hex): return bytes([int("0x%s" % byte, 16) for byte in command0.split()])

command0 = "FF 30 30 01 51 33 34 46 EF" str_hex_to_byte(command0) command0 = "FF 30 30 01 51 33 34 46 EF" str_hex_to_byte(command0)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM