Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
696 views
in Technique[技术] by (71.8m points)

python - int.to_bytes OverflowError:int太大,无法转换为1个字节(int.to_bytes OverflowError:int too big to convert when convering to 1 byte)

I'm trying to convert an int to bytes, but trying to convert under 4 bytes raises OverflowError.

(我正在尝试将int转换为字节,但是尝试将其转换为4个字节以下会引发OverflowError。)

What is the reason for this?

(这是什么原因呢?)

a = 6901571584
a.to_bytes(1, byteorder=sys.byteorder)
# OverflowError: int too big to convert

edit: For 5 bytes it returns b'\x00\xa0]\x9b\x01'

(编辑:对于5个字节,它返回b'\x00\xa0]\x9b\x01')

  ask by Zerg Overmind translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The issue was with me missing the important part of the documentation.

(我的问题是缺少文档的重要部分。)

length parameter must be enough to represent the full number.

(length参数必须足以代表完整数字。)

The integer is represented using length bytes.

(整数使用长度字节表示。)

An OverflowError is raised if the integer is not representable with the given number of bytes.

(如果整数不能用给定的字节数表示,则会引发OverflowError。)


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...