打包一个三字节的int [英] pack a three byte int

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

问题描述

Python不能表达三字节int的想法吗?


例如,在下面的工作示例中,我们可以以某种方式崩溃

struct.pack三次调用一次?

Can Python not express the idea of a three-byte int?

For instance, in the working example below, can we somehow collapse the
three calls of struct.pack into one?


>> import struct

skip = 0x123456; count = 0x80

cdb =''''
cdb + = struct.pack(''> B'',0x08)
cdb + = struct.pack(' '>我',跳过)[ - 3:]
cdb + = struct.pack(''> BB'',伯爵,0)

打印'''' .join([''%02X''%ord(xx)for xx in cdb])
>>import struct

skip = 0x123456 ; count = 0x80

cdb = ''''
cdb += struct.pack(''>B'', 0x08)
cdb += struct.pack(''>I'', skip)[-3:]
cdb += struct.pack(''>BB'', count, 0)

print '' ''.join([''%02X'' % ord(xx) for xx in cdb])



08 12 34 56 80 00

08 12 34 56 80 00


>>>
>>>



我问,因为我正在尝试重构简洁的工作代码:

I ask because I''m trying to refactor working code that is concise:


>> cdb0 =''\ x08''''\ x12 \ x34 \ x56'''' \x80''''\'''
print''''。join([''%02X''%ord(xx)for xx in cdb0])
>>cdb0 = ''\x08'' ''\x12\x34\x56'' ''\x80'' ''\0''
print '' ''.join([''%02X'' % ord(xx) for xx in cdb0])



08 12 34 56 80 00

08 12 34 56 80 00


>>>
>>>



在此先感谢Pat LaVarre

Thanks in advance, Pat LaVarre

推荐答案

p.*******@ieee.org 写道:

Python不能表达三字节int的想法吗?
Can Python not express the idea of a three-byte int?



有点难以确定(修辞?)问题的含义。

可能的答案:

1.不像单字节结构代码那样简洁 - 因为你可能已经通过阅读手册确定了

...

2.不,但是当24位机器变得像在20世纪60年代那样受欢迎,随时提交增强请求:-)

It is a bit hard to determine what that (rhetorical?) question means.
Possible answers:
1. Not as concisely as a one-byte struct code -- as you presumably have
already determined by reading the manual ...
2. No, but when 24-bit machines become as popular as they were in the
1960s, feel free to submit an enhancement request :-)


>

例如,在下面的工作示例中,我们可以以某种方式将struct.pack的三次调用折叠成一个吗?
>
For instance, in the working example below, can we somehow collapse the
three calls of struct.pack into one?

> import struct

skip = 0x123456; count = 0x80

cdb =''''
cdb + = struct.pack(''> B'',0x08)
cdb + = struct.pack(' '>我',跳过)[ - 3:]
cdb + = struct.pack(''> BB'',伯爵,0)

打印'''' .join([''%02X''%ord(xx)for xx in cdb])
>import struct

skip = 0x123456 ; count = 0x80

cdb = ''''
cdb += struct.pack(''>B'', 0x08)
cdb += struct.pack(''>I'', skip)[-3:]
cdb += struct.pack(''>BB'', count, 0)

print '' ''.join([''%02X'' % ord(xx) for xx in cdb])



08 12 34 56 80 00

08 12 34 56 80 00


>>
>>



您可以尝试在包装之前抛弃多余的部分而不是

之后:


| >>来自struct import pack

| >> skip = 0x123456; count = 0x80

| >> hi,lo = divmod(skip,0x10000)

| >> cdb = pack("> BBHBB",0x08,hi,lo,count,0)

| >>''''。join([&x;%02X"%ord(x)for x in cdb])

| ''08 12 34 56 80 00''


但为什么要这样做才能简化工作代码???

You could try throwing the superfluous bits away before packing instead
of after:

| >>from struct import pack
| >>skip = 0x123456; count = 0x80
| >> lo = divmod(skip, 0x10000)
| >>cdb = pack(">BBHBB", 0x08, lo, count, 0)
| >>'' ''.join(["%02X" % ord(x) for x in cdb])
| ''08 12 34 56 80 00''

but why do you want to do that to concise working code???

< br>

文章< 11 ********************** @ k70g2000cwa.googlegroups .com>,
p。******* @ ieee.org 写道:
In article <11**********************@k70g2000cwa.googlegroups .com>,
p.*******@ieee.org wrote:

Python可以不表达三字节int的想法吗?

例如,在下面的工作示例中,我们可以以某种方式崩溃

三struct.pack调用一个?
Can Python not express the idea of a three-byte int?

For instance, in the working example below, can we somehow collapse the
three calls of struct.pack into one?

> import struct

skip = 0x123456; count = 0x80

cdb =''''
cdb + = struct.pack(''> B'',0x08)
cdb + = struct.pack(' '>我',跳过)[ - 3:]
cdb + = struct.pack(''> BB'',count,0)
>import struct

skip = 0x123456 ; count = 0x80

cdb = ''''
cdb += struct.pack(''>B'', 0x08)
cdb += struct.pack(''>I'', skip)[-3:]
cdb += struct.pack(''>BB'', count, 0)



为什么不是这样的:


skip + = struct.pack("> L",skip)[1:]


Dave

Why not something like this:

skip += struct.pack(">L", skip)[1:]

Dave


对不起,应该是:


cdb + = struct .pack("> L",skip)[1:]


Dave
Sorry, that should have been:

cdb += struct.pack(">L", skip)[1:]

Dave


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

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