本文介绍了打包一个三字节的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 thethree calls of struct.pack into one? 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: 08 12 34 56 80 00 08 12 34 56 80 00 在此先感谢Pat LaVarre Thanks in advance, Pat LaVarre 推荐答案 有点难以确定(修辞?)问题的含义。 可能的答案: 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 havealready determined by reading the manual ...2. No, but when 24-bit machines become as popular as they were in the1960s, feel free to submit an enhancement request :-) 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 insteadof 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??? 为什么不是这样的: skip + = struct.pack("> L",skip)[1:] Dave Why not something like this: skip += struct.pack(">L", skip)[1:] Dave 这篇关于打包一个三字节的int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!
10-20 16:07