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

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

问题描述

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

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


推荐答案

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

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

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

或在Python 2和Python 3中:

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.

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

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