本文介绍了2.3 - > 2.4:long int太大而无法转换为int的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我放弃了,如何在2.4下使其失败? fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck(" HBB,0x1c,0x00,0x00)) 我得到一个OverflowError:long int太大而无法转换为int ioctl()期望一个32位整数值,并且0xc0047a80具有 高位设置。我假设Python认为它是一个 签名值。我如何告诉Python 0xc0047a80是一个 无符号32位值? - Grant Edwards grante哇!我要求IMPUNITY! visi.com 解决方案 在2.3和之前,你得到这个: -1073448320 在2.4中,正十六进制文字被视为正数,这就是你的问题:你的文字大于最大的int,因此得到 存储为长整数。我会尝试-1073448320作为arg。 Terry J. Reedy 你可以这样伪装它, def unsigned(val): 返回struct.unpack(''我',struct.pack(''我',val))[0] fcntl.ioctl(self.dev.fileno(),unsigned(0xc0047a80),...) 但写好文档字符串解释好运为什么一个函数叫做 " unsigned"取正数并返回负数...;) 克里斯珀金斯 -1073448320 我并不特别关心Python如何打印价值 - 我只是 想要传递给我正在调用的函数的值。 在2.4中,正十六进制文字被视为正数,这就是你的问题:你的文字更大比最大的int因此得到存储为long int。 我知道,我只是想出一个很好的方法来解决它。 我会尝试-1073448320作为arg。 这应该有效,但它有点蹩脚(没有冒犯)。 ioctl值总是,总是用十六进制写的。一块 ioctl值通常分配给特定的驱动程序,例如高阶N(是4个5?)十六进制数字对于 那个司机。用十进制写的值是 完全混淆任何看代码的人。 我更喜欢写一个函数的另一个建议 接受0x< whatever>并返回相应的整数值。 另一张海报建议使用struct的解决方案。这是我的 解决方案(假设python整数用2'的 恭维二进制表示): def ioctlValue(i): if i& 0x80000000: i = - ((i ^ 0xffffffff)+1) 返回i - 格兰特爱德华兹格兰特哇!在Tenafly的某个地方, 在新泽西州,一名脊椎按摩师 visi.com正在查看留下它来b / b eaver! I give up, how do I make this not fail under 2.4?fcntl.ioctl(self.dev.fileno(),0xc0047a80,struct.pa ck("HBB",0x1c,0x00,0x00))I get an OverflowError: long int too large to convert to intioctl() is expecting a 32-bit integer value, and 0xc0047a80 hasthe high-order bit set. I''m assuming Python thinks it''s asigned value. How do I tell Python that 0xc0047a80 is anunsigned 32-bit value?--Grant Edwards grante Yow! I demand IMPUNITY!atvisi.com 解决方案In 2.3 and before, you get this:-1073448320In 2.4, positive hex literals are treated as positive numbers, and that isyour problem: your literal is greater than the largest int and hence getsstored as long int. I would try -1073448320 as the arg.Terry J. ReedyYou could sort-of fake it like this,def unsigned(val):return struct.unpack(''i'', struct.pack(''I'', val))[0]fcntl.ioctl(self.dev.fileno(), unsigned(0xc0047a80), ...)but good luck writing a docstring explaining why a function called"unsigned" takes a positive long and returns a negative int... ;)Chris Perkins -1073448320I don''t particular care how Python prints the value -- I justwant that value passed to the function I''m calling. In 2.4, positive hex literals are treated as positive numbers, and that is your problem: your literal is greater than the largest int and hence gets stored as long int.I knew that, I just couldn''t come up with a good way to fix it. I would try -1073448320 as the arg.That should work, but it''s kind of lame (no offense).ioctl values are always, always written in hex. A block ofioctl values is generally assigned to a particular driver suchthat the high order N (is it 4 oe 5?) hex digits are unique tothat driver. Writing the value in decimal is going tocompletely confuse anybody looking at the code.I rather like the other suggestion of writing a function thataccepts 0x<whatever> and returns the appropriate integer value.Another poster suggested a solution using struct. Here''s mysolution (which assume python integers are represented in 2''scompliment binary):def ioctlValue(i):if i & 0x80000000:i = -((i^0xffffffff)+1)return i--Grant Edwards grante Yow! Somewhere in Tenafly,at New Jersey, a chiropractorvisi.com is viewing "Leave it toBeaver"! 这篇关于2.3 - > 2.4:long int太大而无法转换为int的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 09-02 15:14