python 2.7 相当于内置方法 int.from_bytes [英] python 2.7 equivalent of built-in method int.from_bytes

查看:36
本文介绍了python 2.7 相当于内置方法 int.from_bytes的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使我的项目 python2.7 和 3 兼容,并且 python 3 具有内置方法 int.from_bytes.python 2.7 中是否存在等效项,或者更确切地说,使此代码 2.7 和 3 兼容的最佳方法是什么?

<预><代码>>>>int.from_bytes(b"f483", byteorder="big")1714698291

解决方案

您可以将其视为一种编码(特定于 Python 2):

<预><代码>>>>int('f483'.encode('hex'), 16)1714698291

或者在 Python 2 和 Python 3 中:

<预><代码>>>>int(codecs.encode(b'f483', 'hex'), 16)1714698291

优点是字符串不限于特定的大小假设.缺点是它是未签名的.

I'm trying to make my project python2.7 and 3 compatible and python 3 has the built in method int.from_bytes. Does the equivalent exist in python 2.7 or rather what would be the best way to make this code 2.7 and 3 compatible?

>>> int.from_bytes(b"f483", byteorder="big")
1714698291

解决方案

You can treat it as an encoding (Python 2 specific):

>>> int('f483'.encode('hex'), 16)
1714698291

Or in Python 2 and Python 3:

>>> int(codecs.encode(b'f483', 'hex'), 16)
1714698291

The advantage is the string is not limited to a specific size assumption. The disadvantage is it is unsigned.

这篇关于python 2.7 相当于内置方法 int.from_bytes的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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