Python 2.5 版本的 array.array.tostring()?

发布于 2024-11-15 18:08:58 字数 471 浏览 5 评论 0原文

我在 GAE 上使用 2.5,使用 gdata python 模块中的一些加密模块。我可以在字符串上使用 hashAndSign,但该函数返回“B”无符号字符的 array.array。

我仍在尝试将它们组合在一起,但我现在遇到的一件事是得到 an:

array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 124, 154, 99, 179])

成为字符串表示形式,例如:

'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'

如何使数组成为这样的字符串?

我知道这是一件简单的事情,但它却难倒了我。

我需要这个,这样我就可以执行 .encode('base64') 发送到我的客户端应用程序,希望能够使用它来验证我签名的字符串。

I am using 2.5 on GAE using some of the Crypto modules from gdata python modules. I am able to use hashAndSign on a string, but the function returns an array.array of 'B' unsigned characters.

I'm still experimenting on getting it all together, but one thing I'm stumbling on now is getting an:

array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 124, 154, 99, 179])

to be a string representation like:

'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'

How can I get the array to be a string like that?

I know this is a simple thing, but it is stumping me.

I need this so I can do a .encode('base64') to send to my client app which hopefully will be able to use it to verify the string I signed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

长亭外,古道边 2024-11-22 18:08:58

如果由于某种原因 .tostring() 方法不可用——这让我认为这个数组与通常的 array.array 不同,因为 .tostring() 至少可以追溯到 python 2.4 - 然后

>>> import array
>>> q = array.array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 124, 154, 99, 179])
>>> ''.join(chr(c) for c in q)
'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'

应该可以工作。

If for some reason the .tostring() method isn't available -- which makes me think that this array is different from the usual array.array, because .tostring() was around at least as far back as python 2.4 -- then

>>> import array
>>> q = array.array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 124, 154, 99, 179])
>>> ''.join(chr(c) for c in q)
'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'

should work.

冰魂雪魄 2024-11-22 18:08:58

您的问题标题“Python 2.5 版本的 array.array.tostring()?”令人费解。 array.array 对象的 tostring() 方法已经存在很长时间了:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import array
>>> q = array.array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 124, 154, 99, 179])
>>> q.tostring()
'x\3471\002}\217\325\016\326B4\013|\232c\263'
>>>

旁白:是的,没错,从前,repr( ) 是八进制,而不是十六进制。

您在 Python 2.5 中实际尝试了什么?结果如何?将完整的堆栈跟踪和错误消息复制/粘贴到问题的编辑中。

Your question title "Python 2.5 version of array.array.tostring()?" is puzzling. The tostring() method of array.array objects has been available for a very long time:

Python 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
>>> import array
>>> q = array.array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 124, 154, 99, 179])
>>> q.tostring()
'x\3471\002}\217\325\016\326B4\013|\232c\263'
>>>

Aside: Yes, that's right, once upon a time, repr() did octal, not hex.

What did you actually try in Python 2.5? What was the outcome? Copy/paste the full stack trace and error message into an edit of your question.

紫罗兰の梦幻 2024-11-22 18:08:58

或者只是 array.tostring 方法:

>>> a = array.array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 1
24, 154, 99, 179])
>>> a.tostring()
'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'

Or just array.tostring method:

>>> a = array.array('B', [120, 231, 49, 2, 125, 143, 213, 14, 214, 66, 52, 11, 1
24, 154, 99, 179])
>>> a.tostring()
'x\xe71\x02}\x8f\xd5\x0e\xd6B4\x0b|\x9ac\xb3'
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文