Python 3-从int转换为“字节”,然后将它们串联(用于串行传输) [英] Python 3 - on converting from ints to 'bytes' and then concatenating them (for serial transmission)

查看:361
本文介绍了Python 3-从int转换为“字节”,然后将它们串联(用于串行传输)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过无济于事的搜索之后,我遇到了一个非常具体的问题,即了解Python 3.2中处理字节和十六进制内容的方式。我知道我误会了,但是似乎找不到正确的路径。



我的最终目标是使用python串行模块传输字节序列。有些字节是静态的,不会改变。其他值应该在0-255之间变化。这些都需要一起涂抹并立即传播。 (这些是到可编程显示器的指令。代码包含固定的指令,用于设置BG颜色,其后分别为R,G和B值提供一个字节。我试图在一个循环中循环测试颜色强度,但是稍后我会



完整的静态传输(经测试可以成功工作)可能如下:

  ser.write(b'\xAA\x11\x05\x00\x00\x00\x00\xc3')#this效果很好

类似地,我可以将它们混在一起,即:

  ser.write(b'\xAA\x11\x05'+ b'\x00\x00\x00\xc3')#还要现在效果不错

现在,如果我想获取这三个零值字节之一,并替换为一个变量,全部变成梨形。经过大量实验,我最终得到了一些东西,据称该东西将For循环的整数变量转换为与上述一系列字节串联的兼容类型:

  SET_BG_COLOR = b'\xAA\x03\x03'
对于范围(0,255)中的r:
red = hex(r).encode('utf-8')
blue = hex(255-r).encode('utf-8')
ser.write(SET_BG_COLOR + blue + b'\x00'+ red + b'\xC3')# BGR格式

十六进制(integer).encode('utf-8')是唯一的方法,因此到目前为止,这不仅引发了关于无法连接到我试图推低串行连接的其他问题的错误。但这不起作用,并且在查看结果时:

 >> x = b’\05’
>>> x
b’\x05’
>>> y = hex(5).encode('utf-8')
>>> y
b’0x5’
>>> type(x)
< class'bytes'>
>>输入(y)
< class'bytes'>
>> x + y
b’\x050x5’#(这就是我得到的)
>>> z = b’\05’
>>> x + z
b’\x05\x05’#(这就是我想要的)
>>>

看起来像,尽管它可以让我连接起来……它是字符串数据的二进制表示形式,或诸如此类?我可以串联一下,但这不是真正的十六进制值吗?我是否错过了从x = 255到x = b'\FF'的令人眼花obvious乱的明显方法?还是我的整个方法只是做这件事的错误方法? -_-谢谢您的时间。

解决方案

您在这里混淆了Python字节文字语法;您不需要生成文字语法,只需生成字节值即可; bytes()类型也接受一系列整数

 >>> bytes([255])
b'\xff'

适用于您的代码:

  SET_BG_COLOR = b'\xAA\x03\x03'
对于范围在(0,255)中的r:
红色=字节([r])
蓝色=字节([255-r])
ser.write(SET_BG_COLOR +蓝色+ b'\x00'+红色+ b'\ \xC3')#BGR格式

或更简单:

  SET_BG_COLOR = [0xAA,0x03,0x03] 
对于范围(0,255)中的r:
ser.write(bytes(SET_BG_COLOR + [r ,0x00,255-r,0xC3])))#BGR格式

使用立即数十六进制整数表示法。 / p>

Demo for r = 10

 >> SET_BG_COLOR = [0xAA,0x03,0x03] 
>> r = 10
>> bytes(SET_BG_COLOR + [r,0x00,255-r,0xC3])
b'\xaa\x03\x03\n\x00\xf5\xc3'

hex()函数每字节输出4个字符;以文字 0x 开头,后跟整数的十六进制表示形式。编码为仍为4个字节的UTF8, b'\x30\x78\x30\x31'表示整数值 10 b'\x10'表示所需的实际字节。


After much fruitless searching... I am having a very specific issue understanding the way 'bytes' and hexadecimal content is handled in Python 3.2. I KNOW I'm misunderstanding, but can't seem to find the correct path.

My ultimate goal is to use the python serial module to transmit a sequence of bytes. Some bytes are static and won't change. Others are supposed to vary from 0-255 in value. These all need to be smushed together and transmitted at once. (These are instructions to a programmable display. The code contains fixed instructions to set BG colour, followed by a byte each for R, G and B values. I am trying to cycle through colour intensities in a loop to test, but later I'll want to be able to do this for practical functions on the display).

A complete static transmission, tested working successfully, might be as follows:

ser.write(b'\xAA\x11\x05\x00\x00\x00\xc3')  #this works great

Similarly, I can smush them together, i.e:

ser.write(b'\xAA\x11\x05' + b'\x00\x00\x00\xc3')  #also works great

Now if I want to take one of those three zero-value bytes, and replace it with a variable, it all goes pear-shaped. After much experimentation I ended up with something which allegedly converted the integer variable of a For loop into a type compatible with concatenation to the above series of bytes:

SET_BG_COLOR = b'\xAA\x03\x03'
for r in range(0,255):
            red = hex(r).encode('utf-8')
            blue = hex(255-r).encode('utf-8')
            ser.write(SET_BG_COLOR + blue + b'\x00' + red + b'\xC3') #BGR format

The hex(integer).encode('utf-8') was the only method so far which didn't just throw an error about being unable to concatenate to the other stuff I'm trying to shove down the serial connection. But it doesn't work, and when looking at the results:

>>> x = b'\05'
>>> x
b'\x05'
>>> y = hex(5).encode('utf-8')
>>> y
b'0x5'
>>> type(x)
<class 'bytes'>
>>> type(y)
<class 'bytes'>
>>> x + y
b'\x050x5'   #(this is what I get)
>>> z = b'\05'
>>> x + z
b'\x05\x05'  #(this is what I want)
>>> 

Looks like, although it lets me concatenate... it's a binary representation of string data, or somesuch? So will let me concatenate but it's not true hex values? Have I missed a blindingly obvious way to go from x=255 to x= b'\FF'? Or is my whole approach just the wrong way to do this? -_- thanks for your time.

解决方案

You are confusing Python byte literal syntax here; you do not need to generate the literal syntax, just the byte value; the bytes() type accepts a sequence of integers too:

>>> bytes([255])
b'\xff'

Applied to your code:

SET_BG_COLOR = b'\xAA\x03\x03'
for r in range(0,255):
    red = bytes([r])
    blue = bytes([255 - r])
    ser.write(SET_BG_COLOR + blue + b'\x00' + red + b'\xC3') #BGR format

or, simpler still:

SET_BG_COLOR = [0xAA, 0x03, 0x03]
for r in range(0,255):
    ser.write(bytes(SET_BG_COLOR + [r, 0x00, 255 - r, 0xC3])) #BGR format

using literal hex integer notation.

Demo for r = 10:

>>> SET_BG_COLOR = [0xAA, 0x03, 0x03]
>>> r = 10
>>> bytes(SET_BG_COLOR + [r, 0x00, 255 - r, 0xC3])
b'\xaa\x03\x03\n\x00\xf5\xc3'

The hex() function outputs 4 characters per byte; starting with a literal 0x followed by the hex representation of the integer number. Encoded to UTF8 that's still 4 bytes, b'\x30\x78\x30\x31' for the integer value 10, for example, versus b'\x10' for the actual byte you wanted.

这篇关于Python 3-从int转换为“字节”,然后将它们串联(用于串行传输)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆