简体   繁体   English

python:“ L”格式代码的整数超出范围

[英]python: integer out of range for 'L' format code

In python, the code is the following 在python中,代码如下

envimsg = struct.pack("!LHL", 1, 0, int(jsonmsg["flow_id"], 16)) + \
          struct.pack("!HQH", 1, int(flow["src id"],16), 0) + \
          struct.pack("!HQH", 1, int(flow["dst id"],16), int(flow["dst port"],16)) + \
          struct.pack("!H", 0) + \
          struct.pack("!HHHLL", int(jsonmsg["app_src_port"],10), int(jsonmsg["app_dst_port"],10), int(jsonmsg["app_proto"],10), int(jsonmsg["app_src_ip"],10), int(jsonmsg["app_dst_ip"],10))

at the line 在行

struct.pack("!H", 0) + \

I encounter this error: 我遇到此错误:

  File "./Translate_2503.py", line 205, in lavi2envi
    struct.pack("!H", 0) + \
struct.error: integer out of range for 'L' format code

which is strange because I try to pack in H (unsigned short). 这很奇怪,因为我尝试用H打包(无符号短)。

Any clues? 有什么线索吗?

My python version 2.7.3. 我的python版本2.7.3。 CPU archi is 32bit. CPU架构为32位。

Even in the error line is pointing to this line, the error is not located there. 即使在错误行指向该行,错误也不位于此处。 Executing this instruction in the Python interpreter produces no error: 在Python解释器中执行此指令不会产生错误:

import struct
struct.pack("!H", 0)
>>> '\x00\x00'

This makes sense, as the error is complaining on the 'L' format code, so the error will be located in the ones which use this format. 这是有道理的,因为错误正在抱怨“ L”格式代码,因此错误将位于使用该格式的代码中。

Given that 'L' is used for unsigned long, and the message complains on being out of range, the error is because one (or more) of the variables used are negative, producing the out of range for unsigned long. 给定“ L”用于无符号长整型,并且该消息抱怨超出范围,则错误是因为所使用的一个(或多个)变量为负,导致无符号长整超出范围。

This can be verified in the Python interpreter: 可以在Python解释器中进行验证:

import struct

struct.pack("!HHHLL", 1, 2, 3, 4, 5)
>>> '\x00\x01\x00\x02\x00\x03\x00\x00\x00\x04\x00\x00\x00\x05'

struct.pack("!HHHLL", 1, 2, 3, -4, 5)
>>> Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
struct.error: integer out of range for 'L' format code

Most likely the problem is in the value of one of these: 问题最有可能是以下其中一项的价值:

jsonmsg["flow_id"]
jsonmsg["app_src_ip"]
jsonmsg["app_dst_ip"]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM