问题描述
众所周知,计算机使用数字。我现在正在键入此文本,服务器会从中输入一个数字,当您想要阅读它时,您将从服务器获取文本。
As we all know, a computer works with numbers. I'm typing this text right now, the server makes a number out of it and when you want to read it, you'll get text from the server.
我如何独自完成这项工作?
How can I do this on my own?
我想用我自己的算法加密一些东西,我的算法可以很好地处理整数,但现在我想加密一个字符串,我不知道怎么转换一个Unicode字符串为整数,反之亦然。
I want to encrypt something with my own algorithm and my algorithm works fine with integers, but now I want to encrypt a String and I don't know how to convert a Unicode string to an integer number and vice versa.
我正在使用Python 3.有没有人知道我的问题的优雅解决方案?
I'm using Python 3. Is there anybody who knows an elegant solution for my problem?
推荐答案
您正在寻找,我认为:
You are looking for the ord()
function, I think:
>>> ord('a')
97
>>> ord('\u00c2')
192
要转换一整套字符,请使用列表理解:
To convert a whole set of characters use a list comprehension:
>>> [ord(c) for c in 'Hello World!']
[72, 101, 108, 108, 111, 32, 87, 111, 114, 108, 100, 33]
它的反面是:
It's inverse is the chr()
function:
>>> chr(97)
'a'
>>> chr(193)
'Á'
这篇关于Python3将Unicode String转换为int表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!