Python 3 to_bytes 是否已向后移植到 Python 2.7? [英] Has Python 3 to_bytes been back-ported to python 2.7?

查看:39
本文介绍了Python 3 to_bytes 是否已向后移植到 Python 2.7?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我追求的功能:-

http://docs.python.org/3/library/stdtypes.html#int.to_bytes

我需要大字节序支持.

推荐答案

根据@nneonneo 的回答,这里有一个模拟 to_bytes API 的函数:

Based on the answer from @nneonneo, here is a function that emulates the to_bytes API:

def to_bytes(n, length, endianess='big'):
    h = '%x' % n
    s = ('0'*(len(h) % 2) + h).zfill(length*2).decode('hex')
    return s if endianess == 'big' else s[::-1]

这篇关于Python 3 to_bytes 是否已向后移植到 Python 2.7?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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