写字节 [英] Writing bytes out

查看:90
本文介绍了写字节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 Pyserial 将字节写入连接.

I am using Pyserial to write out bytes to a connection.

我一直在努力解决的一件事是弄清楚如何准确地写出我想要的位.我已经成功写出字符串 ascii 形式的字节,例如

One of the things I have been struggling with is figuring out how to write out exactly the bits that I want. I have had success writing out the bytes of a strings ascii form, such as

variable = 'h'.encode()
serial.write(variable)

这将成功地写出 H 的位.但是,如果我用 b'\b001' 做类似的事情,它不会像我希望的那样写出 001.

And that will write out the bits of H succesfully. However if I do something like that with b'\b001' it won't write out 001 like I had hoped it would.

有没有办法做到这一点,所以我可以简单地使用 1 和 0 定义变量,然后使用 write 函数.

Is there a way to do this so I can simply define variables using 1 and 0 and then use the write function.

我也尝试过这样定义:

y = 0x13
out = s.write(y)

根据 write 函数的文档,该函数返回写入的字节数.当我运行这个特定的代码时,它返回了 19

Which according to the write function's documentation, the function returns the amount of bytes written. When I ran this specific code it returned 19

推荐答案

如果我理解正确,您正在尝试找到一种方法将二进制数据作为 python 字节对象发送,并将十六进制数据作为 python 字节对象发送(在第二部分).

If I understand correctly you are trying to find a way to send binary data as python bytes objects, and hex data as python bytes objects (in the second part).

它可能违反直觉,但 python b'' 语法不代表 二进制,但字节 因此为什么 b'0010101' 不能像您想要的那样工作.

it may be counter intuitive, but pythons b'' syntax doesn't stand for binary, but bytes hence why b'0010101' doesn't work like you want.

Bytes 默认接受十六进制数据作为字节,为此,您需要将十六进制数据格式化为 b'\xFF\x04'.例如

Bytes accepts hex data as bytes by default, to do this, you need to format the hex data as b'\xFF\x04'. For example

byte_obj = bytes(b'\xFF\x04')

这可以通过pyserial发送.要进行二进制,您需要使用 python int 的构造方法,即 int(number, base) 方法.

This can then be sent over pyserial. To do binary you'll need to use python int's construction methods, ie the int(number, base) method.

进一步解释了这里

但这基本上可以通过

bin_to_int int('01010101', 2)
byte_obj = bytes([bin_to_int]) #requires array of ints

不幸的是,这只适用于大小为 8 位的二进制字符串(它会给你一个错误告诉你).为了解决这个问题,您可以执行以下操作:

Unfortunately that only works for binary strings of size 8 bits (and it will give you an error telling you so). In order to get around this, you can do the following:

bin_to_int int('0101010101010101', 2)
# to_bytes takes size in bytes and endianess, which is the order the bytes are placed.
byte_obj = bin_to_int.to_bytes(2, 'little')

little endian 是 最小地址中的最低有效字节(即索引0)大相反

little endian is least significant byte in smallest address (ie index 0) big is opposite

注意较大的二进制整数方法,我不确定python在内部如何将二进制转换为整数,但是对于任意大的二进制数据,我怀疑由于我如何巧妙地实现转换而变慢.我会给你一个例子,说明为什么我会做二进制到无符号整数的转换:

Be careful of the larger binary integer method, I'm not sure what python does internally to convert binary to an integer, but for arbitarily large binary data I would suspect slow down due to how I would niavely implement the conversion. I'll give you an example of why in how I would do the binary to unsigned integer conversion:

bit_string = "101010101..."
exponent = 0
decimal_value = 0
for bit in bit_string:
    decimal_value += int(bit)**exponent
    exponent += 1

一开始可能不是很明显,但是由于python ints的大小是任意的,最终你将达到将非常大的ints加在一起的程度,超出了典型的算术int大小(即大于64位).这意味着随着时间的推移,对于大的二进制值(您可能会产生),您将获得非线性时间复杂度,因为 N 增加了加法的字节大小以及它们增长到 64 位之外(并且更多的处理将需要完成加法,这将是 O(n_bytes)) 而不是 O(1))

It might not be quite obvious at first, but because python ints are arbitraily sized eventually you are going to get to the point where you are adding very large ints together, outside of typical arithmetic int size (ie larger than 64 bits). This means that over time with large binary values (which you will likely produce) you are going to get non linear time complexity as N increases the size in bytes of the additions will as well as they grow outside of 64 bits (and more processing will need to be done to carry out the addition, which will be O(n_bytes)) instead of O(1))

要解决这个问题,您只需要创建一个函数,在将二进制值转换为字节之前将它们分开(例如 binary_to_bytes(binary_string))

To solve this you would just need to make a function that separates the binary values before converting to bytes with them (like binary_to_bytes(binary_string))

这篇关于写字节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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