python 在python中将整数列表转换为二进制“字符串”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1331187/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting list of integers into a binary "string" in python
提问by mozami
I have a list of numbers that i would like to send out onto a socket connection as binary data.
我有一个数字列表,我想将其作为二进制数据发送到套接字连接上。
As an example, i start off with the following list:
例如,我从以下列表开始:
data = [2,25,0,0,ALPHA,0,23,18,188]
In the above list, ALPHAcan be any value between 1 and 999. Initially, I was converting this into a string using
在上面的列表中,ALPHA可以是 1 到 999 之间的任何值。最初,我使用
hexdata = ''.join([chr(item) for item in data])
So if ALPHA is 101, this would return the following string:
所以如果 ALPHA 是 101,这将返回以下字符串:
>>> data = [2,25,0,0,101,0,23,18,188]
>>> hexdata = ''.join([chr(item) for item in data])
>>> hexdata
'\x02\x19\x00\x00e\x00\x17\x12\xbc'
This works just fine and '\x02\x19\x00\x00e\x00\x17\x12\xbc' is the string that i need to send out.
这工作得很好,'\x02\x19\x00\x00e\x00\x17\x12\xbc' 是我需要发送的字符串。
However, this does not work for values of ALPHA that are over 255 because its out of range of the chr statement. If for example ALPHA were 999, then i would like to get the following string:
但是,这不适用于超过 255 的 ALPHA 值,因为它超出了 chr 语句的范围。例如,如果 ALPHA 是 999,那么我想得到以下字符串:
data = [2,25,0,0,999,0,23,18,188]
hexdata = '\x02\x19\x00\x03\xed\x00\x17\x12\xbc'
Ive been looking at the documentation on the struct.pack() but cannot see how that could be used to acheive the above string. ALPHA is the only variable in the list.
我一直在查看 struct.pack() 上的文档,但看不到如何使用它来实现上述字符串。ALPHA 是列表中的唯一变量。
Any help would be greatly appreciated.
任何帮助将不胜感激。
EDIT 1
编辑 1
What behavior do you want? Anything between 256 and 65535 takes 2 bytes to represent. Do you want to unpack it on the other side? Please update the post with your intent. – gahooa 1 min ago
你想要什么行为?256 到 65535 之间的任何东西都需要 2 个字节来表示。你想在另一边拆开它吗?请根据您的意图更新帖子。– gahooa 1 分钟前
Thats correct, since 999 is over the 256 threshold, its represented by two bytes:
没错,因为 999 超过了 256 阈值,所以它由两个字节表示:
data = [2,25,0,0,999,0,23,18,188]
hexdata = '\x02\x19\x00**\x03\xed**\x00\x17\x12\xbc'
Does this make sense?
这有意义吗?
As far as unpacking is concerned, im only sending this data out onto the socket, I will be receiving data but thats taken care of already.
就解包而言,我只将这些数据发送到套接字上,我将接收数据,但这已经处理好了。
EDIT 2
编辑 2
The string i send out is always fixed length. For simplicity, I think its better to represent the list as follows:
我发出的字符串总是固定长度的。为简单起见,我认为最好将列表表示如下:
ALPHA = 101
data = [25,alpha1,alpha2,1]
hexdata = '\x19\x00e\x01'
ALPHA = 301
data = [25,alpha1,alpha2,1]
hexdata = 'x19\x01\x2d\x01'
as you can see in the hexdata string, this then becomes: \x01\x2d\
正如您在十六进制数据字符串中看到的那样,这将变为:\x01\x2d\
If ALPHA < 256, alpha1 = 0.
如果 ALPHA < 256,则 alpha1 = 0。
回答by Alex Martelli
You want to send a single byte for ALPHA if it's < 256, but two bytes if >= 256? This seems weird -- how is the receiver going to know which is the case...???
如果小于 256,您想为 ALPHA 发送一个字节,如果 >= 256,则为两个字节?这看起来很奇怪——接收者如何知道这是什么情况……???
But, if this IS what you want, then
但是,如果这就是你想要的,那么
x = struct.pack(4*'B' + 'HB'[ALPHA<256] + 4*'B', *data)
is one way to achieve this.
是实现这一目标的一种方式。
回答by Ants Aasma
If you know the data and ALPHA position beforehand, it would be best to use struct.pack with a big endian short for that position and omit the 0 that might be overwritten:
如果您事先知道数据和 ALPHA 位置,最好使用 struct.pack 和该位置的大端短,并省略可能被覆盖的 0:
def output(ALPHA):
data = [2,25,0,ALPHA,0,23,18,188]
format = ">BBBHBBBB"
return struct.pack(format, *data)
output(101) # result: '\x02\x19\x00\x00e\x00\x17\x12\xbc'
output(999) # result: '\x02\x19\x00\x03\xe7\x00\x17\x12\xbc'
回答by Nadia Alramli
回答by Lennart Regebro
Bytes can not have a value over 255, so with ALPHA being over 255 you are going to somehow have to split it into two bytes. This can be done like so:
字节的值不能超过 255,因此当 ALPHA 超过 255 时,您将不得不以某种方式将其拆分为两个字节。这可以像这样完成:
high, low = divmod(ALPHA, 255)
Then you can stick the high and low into the list of values.
然后,您可以将最高和最低值粘贴到值列表中。
There are other variations like using bytearray (in 2.6) or struct.pack, etc, but in the end you will have to convert that number two two bytes somehow.
还有其他变体,例如使用 bytearray(在 2.6 中)或 struct.pack 等,但最终您必须以某种方式将两个字节转换为两个字节。
回答by formiaczek
You want to send a single byte for ALPHA if it's < 256, but two bytes if >= 256?
如果小于 256,您想为 ALPHA 发送一个字节,如果 >= 256,则为两个字节?
another (perhaps more generic) way would be:
另一种(也许更通用)的方法是:
x = reduce(lambda x, y: x + y, map(lambda x: struct.pack('B', x), data))