在 Python 中將 Int 轉換為 ASCII
 
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)
輸出:
e
在上面的程式碼中,chr() 函式應用於數字 101,它提供輸出 e。
在 Python 2 中,也可以使用 unichr() 函式代替 chr() 函式。unichr() 函式可以提供數字的 Unicode 字串。
以下程式碼使用 unichr() 函式在 Python 中將 int 轉換為 ASCII。
# Python 2
a = unichr(101)
print(a)
輸出:
e
在較新版本的 Python(Python 3 及更高版本)中,普通字串和 Unicode 之間沒有對比。
要將 ASCII 值轉換回 int,我們可以使用 ord() 函式。ord() 的通用目的是獲取單位長度的字串並提供作為引數傳遞的字串的 Unicode 等價性。
ord() 函式主要用於實現 chr() 和 unichr() 函式的相反目的。它具有基本語法,是 Python 提供的內建函式之一。
下面的程式碼使用 ord() 函式在 Python 中實現 chr() 函式的逆過程。
print(ord("e"))
輸出:
101
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相關文章 - Python Integer
- 如何在 Python 中把整型轉換為二進位制
- 在 Python 中將布林值轉換為整數
- Python 中反轉整數
- 在 Python 中將字串轉換為整數
- 在 Python 中連線字串和整數值
- 在 Python 中將整數拆分為數字
