如何构造一个16字节长的Python字符串? [英] How can I construct a Python string that is 16 bytes long?

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

问题描述

当我使用一个字符的Python字符串时,即

When I make a Python string of one character, i.e.

txt = "h"

,然后检查其大小,我得到

and I check its size, I get

import sys
sys.getsizeof(txt)
34

如果一个字符是34个字节,我如何制作一个16个字节的字符串?

If one character is 34 bytes, how do I make a String of 16 byes?

PS我正在使用OFB模式下的 PyCrypto AES ,它需要输入的16个字节

P.S. I'm playing around with PyCrypto AES, in OFB Mode, which required input of 16 bytes

推荐答案

getsizeof 不会返回字符串,但它占了额外的垃圾收集器开销。
如下面所示, test 以空字符串开头,但由于上述原因,分配给它的内存不为零。

getsizeof does not return the actual size of the string but it accounts for additional garbage collector overhead. As shown bellow test starts as an empty string but memory allocated for it is not zero due to reasons listed above.

>>> test = ""
>>> sys.getsizeof(test)
37
>>> test = "" + "h"
>>> sys.getsizeof(test)
38
>>> test = "" + "h" + "h"
>>> sys.getsizeof(test)
39
>>>

要创建一个16字节的字符串,一种方法是:

To create a 16 bytes string, one way would be:

>>> test = " " * 16
>>> sys.getsizeof(test)
53

53-37 = 16字节

53 - 37 = 16 bytes

这篇关于如何构造一个16字节长的Python字符串?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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