教程

Python 3 Basic Python Advanced Tkinter Python Modules JavaScript Python Numpy Git Matplotlib PyQt5 Data Structure Algorithm

贴士文章

Rust Python Pygame Python Python Tkinter Batch PowerShell Python Pandas Numpy Python Flask Django Matplotlib Plotly Docker Seaborn Matlab Linux Git C Cpp HTML JavaScript jQuery TypeScript Angular React CSS PHP Java Go Node.js Kotlin Csharp Ruby Arduino MongoDB MySQL Postgres R VBA Scala Raspberry Pi

函数参考

Python Pandas Numpy

ASCII American Standard Code for Information Interchange 的缩写,可以定义为一种标准,可以在包含最多 256 个可用槽的 8 位代码中分配数字、字母和一些其他字符。

本教程将讨论在 Python 中将 int 转换为 ASCII 的不同方法。

在 Python 中使用 chr() 函数将 int 转换为 ASCII

Python 中有用于字符串操作的内置字符串函数。 chr() 函数就是其中之一。

chr() 函数可用于 Python 3 及更高版本,用于提供相应 ASCII 代码编号的 ASCII 值。

以下代码使用 chr() 函数在 Python 中将 int 转换为 ASCII

a = chr(101)
print(a)

在上面的代码中, chr() 函数应用于数字 101 ,它提供输出 e

在 Python 2 中,也可以使用 unichr() 函数代替 chr() 函数。 unichr() 函数可以提供数字的 Unicode 字符串。

以下代码使用 unichr() 函数在 Python 中将 int 转换为 ASCII

# Python 2
a = unichr(101)
print(a)

在较新版本的 Python(Python 3 及更高版本)中,普通字符串和 Unicode 之间没有对比。

要将 ASCII 值转换回 int,我们可以使用 ord() 函数。ord() 的通用目的是获取单位长度的字符串并提供作为参数传递的字符串的 Unicode 等价性。

ord() 函数主要用于实现 chr()unichr() 函数的相反目的。它具有基本语法,是 Python 提供的内置函数之一。

下面的代码使用 ord() 函数在 Python 中实现 chr() 函数的逆过程。

print(ord("e"))
    
                

Vaibhhav is an IT professional who has a strong-hold in Python programming and various projects under his belt. He has an eagerness to discover new things and is a quick learner.

LinkedIn