长<-> str二进制转换 [英] long <-> str binary conversion

查看:101
本文介绍了长<-> str二进制转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否存在仅将很长的数字转换为字符串就复制数据的库?

Is there any lib that convert very long numbers to string just copying the data?

这些单行太慢了:

def xlong(s):
    return sum([ord(c) << e*8 for e,c in enumerate(s)])

def xstr(x):
    return chr(x&255) + xstr(x >> 8) if x else ''

print xlong('abcd'*1024) % 666
print xstr(13**666)


推荐答案

您需要struct模块。

You want the struct module.

packed = struct.pack('l', 123456)
assert struct.unpack('l', packed)[0] == 123456

这篇关于长&lt;-&gt; str二进制转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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