Python3将Unicode String转换为int表示 [英] Python3 convert Unicode String to int representation

查看:413
本文介绍了Python3将Unicode String转换为int表示的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,计算机使用数字。我现在正在键入此文本,服务器会从中输入一个数字,当您想要阅读它时,您将从服务器获取文本。

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?

推荐答案

您正在寻找 ord()功能,我认为:

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]

它的反面是 chr()功能

It's inverse is the chr() function:

>>> chr(97)
'a'
>>> chr(193)
'Á'

这篇关于Python3将Unicode String转换为int表示的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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