如何将字节字符串转换为int? [英] How to convert a string of bytes into an int?

查看:36
本文介绍了如何将字节字符串转换为int?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何在python中将字节字符串转换为int?

这样说:'y\xcc\xa6\xbb'

我想出了一个聪明/愚蠢的方法:

sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))

我知道必须有一些内置或标准库可以更简单地做到这一点......

这与转换一串十六进制数字不同您可以使用 int(xxx, 16),但我想转换一串实际字节值.

更新:

我更喜欢 James 的回答,因为它不需要导入另一个模块,但 Greg 的方法更快:

<预><代码>>>>从 timeit 导入计时器>>>Timer('struct.unpack("<L", "y\xcc\xa6\xbb")[0]', 'import struct').timeit()0.36242198944091797>>>Timer("int('y\xcc\xa6\xbb'.encode('hex'), 16)").timeit()1.1432669162750244

我的hacky方法:

<预><代码>>>>Timer("sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))").timeit()2.8819329738616943

进一步更新:

有人在评论中问导入另一个模块有什么问题.好吧,导入一个模块不一定便宜,看看:

<预><代码>>>>Timer("""import struct\nstruct.unpack(">L", "y\xcc\xa6\xbb")[0]""").timeit()0.98822188377380371

包括导入模块的成本几乎否定了这种方法所具有的所有优势.我相信这只会包括在整个基准测试中导入一次的费用;看看我每次都强制它重新加载时会发生什么:

<预><代码>>>>Timer("""reload(struct)\nstruct.unpack(">L", "y\xcc\xa6\xbb")[0]""", 'import struct').timeit()68.474128007888794

不用说,如果您在每次导入时执行大量此方法,那么这将成比例地减少问题.它也可能是 i/o 成本而不是 cpu,因此它可能取决于特定机器的容量和负载特性.

解决方案

您也可以使用 struct 模块来做到这一点:

<预><代码>>>>struct.unpack("

How can I convert a string of bytes into an int in python?

Say like this: 'y\xcc\xa6\xbb'

I came up with a clever/stupid way of doing it:

sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))

I know there has to be something builtin or in the standard library that does this more simply...

This is different from converting a string of hex digits for which you can use int(xxx, 16), but instead I want to convert a string of actual byte values.

UPDATE:

I kind of like James' answer a little better because it doesn't require importing another module, but Greg's method is faster:

>>> from timeit import Timer
>>> Timer('struct.unpack("<L", "y\xcc\xa6\xbb")[0]', 'import struct').timeit()
0.36242198944091797
>>> Timer("int('y\xcc\xa6\xbb'.encode('hex'), 16)").timeit()
1.1432669162750244

My hacky method:

>>> Timer("sum(ord(c) << (i * 8) for i, c in enumerate('y\xcc\xa6\xbb'[::-1]))").timeit()
2.8819329738616943

FURTHER UPDATE:

Someone asked in comments what's the problem with importing another module. Well, importing a module isn't necessarily cheap, take a look:

>>> Timer("""import struct\nstruct.unpack(">L", "y\xcc\xa6\xbb")[0]""").timeit()
0.98822188377380371

Including the cost of importing the module negates almost all of the advantage that this method has. I believe that this will only include the expense of importing it once for the entire benchmark run; look what happens when I force it to reload every time:

>>> Timer("""reload(struct)\nstruct.unpack(">L", "y\xcc\xa6\xbb")[0]""", 'import struct').timeit()
68.474128007888794

Needless to say, if you're doing a lot of executions of this method per one import than this becomes proportionally less of an issue. It's also probably i/o cost rather than cpu so it may depend on the capacity and load characteristics of the particular machine.

解决方案

You can also use the struct module to do this:

>>> struct.unpack("<L", "y\xcc\xa6\xbb")[0]
3148270713L

这篇关于如何将字节字符串转换为int?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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