# Unicode

**URL:** <https://vip.studycamp.tw/t/unicode/7043>\
**Category:** Python Deep Dive 課程延伸\
**Tags:** 上課筆記\
**Created:** [2024年三月7日 11:13 UTC](https://vip.studycamp.tw/t/unicode/7043 "2024-03-07T11:13:24Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![sky](https://vip.studycamp.tw/user_avatar/vip.studycamp.tw/sky/32/8098_2.png) [@sky](https://vip.studycamp.tw/u/sky)\
**Post date:** [2024年三月7日 11:13 UTC](https://vip.studycamp.tw/t/unicode/7043/1 "2024-03-07T11:13:24Z")

</div>

> 這節課應歸類在 **[Python 3: Fundamentals](https://www.udemy.com/course/python3-fundamentals/)**，而不是 **Python Deep Dive** 。

## ASCII

**ASCII** （ **American Standard Code for Information Interchange** ），是字元編碼標準。

早期電腦系統對未來的想像力有限，覺得只要 128（ 2^7 ）個 **碼點** （code point）就夠了，而且其中只有 95 個是可列印字符，這嚴重限制了它的擴展性。

ASCII 保留前 33 個碼點（十進位數字 0-32、十六進位數字 0x00-0x20）和最後一個碼點（十進位數字 127、十六進位數字 0x7F）作為控製字元。這些代碼旨在控制週邊設備（例如印表機），例如字元 0x0A 代表「換行」功能（打字機紙張前進）。

延伸閱讀：**[csv.writer() 寫入 csv 檔後，每行間會多一空白行](https://vip.studycamp.tw/t/topic/1746)**

現在電腦系統已改為使用有數百萬個（不同資料來源的數字不同，這裡使用 WIKI 資料）碼點的 Unicode，但其中的前 128 個與 ASCII 集相同，以維持相容性。

以下為128 個碼點（0x00 ~ 0x7F）所代表的值：

| | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | A | B | C | D | E | F |
| --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- | --- |
| 0x | | NUL | SOH | STX | ETX | EOT | ENQ | ACK | BEL | BS | HT | LF | VT | FF | CR | SO | SI |
| 1x | | DLE | DC1 | DC2 | DC3 | DC4 | NAK | SYN | ETB | CAN | EM | SUB | ESC | FS | GS | RS | US |
| 2x | | SP | ! | " | # | $ | % | & | ’ | ( | ) | \* | + | , | - | . | / |
| 3x | | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | : | ; | \< | = | \> | ? |
| 4x | | @ | A | B | C | D | E | F | G | H | I | J | K | L | M | N | O |
| 5x | | P | Q | R | S | T | U | V | W | X | Y | Z | [| \ |] | ^ | \_ |
| 6x | | ` | a | b | c | d | e | f | g | h | i | j | k | l | m | n | o |
| 7x | | p | q | r | s | t | u | v | w | x | y | z | { | | | } | ~ | DEL |

 ![ASCII](https://vip.studycamp.tw/uploads/default/original/2X/5/5c12f050d475bc587917214e8bde9bfd769943cd.png)

### ASCII 參考資料：

1. WIKI：**[ASCII - Wikipedia](https://en.wikipedia.org/wiki/ASCII)**

2. 老師講義：**[Python 3: Fundamentals](https://www.udemy.com/course/python3-fundamentals/)** - **Section 6: Strings**

## 十進位、二進位、十六進位

### 十進位及計算公式說明

> Decimal

\begin{array}{|c|c|c|c|} \hline 10^3 & 10^2 & 10^1 & 10^0 \\ \hline 9 & 0 & 3 & 4 \\ \hline \end{array}

計算公式為：

9034 = 4 \times 10^0 + 3 \times 10^1 + 0 \times 10^2 + 9 \times 10^3

### 十六進位及計算公式說明：

> Hexadecimal

\begin{array}{|c|c|c|c|} \hline 16^3 & 16^2 & 16^1 & 16^0 \\ \hline F & C & 1 & 5 \\ \hline \end{array}

計算公式為：

FC15 = 5 \times 16^0 + 1 \times 16^1 + C \times 16^2 + F \times 16^3  
 = 64533（十進位）

### 二進位及計算公式說明：

> Binary

\begin{array}{|c|c|c|c|} \hline 2^3 & 2^2 & 2^1 & 2^0 \\ \hline 1 & 0 & 1 & 1 \\ \hline \end{array}

計算公式為：

1011 = 1 \times 2^0 + 1 \times 2^1 + 0 \times 2^2 + 1 \times 2^3  
 = 11（十進位）

## Unicode

和 ASCII 不同，Unicode 只定義、不編碼。

目前最常用的編碼是 UTF-8（Python 預設使用）。

> UTF: Unicode Transformation Format

### Unicode Character “A” (U+0041)

| **The Character Name** : | Latin Capital Letter A [[1]](https://www.compart.com/en/unicode/U+0041#UNC_DB) |
| --- | --- |
| Unicode Version: | 1.1 (June 1993) [[2]](https://www.compart.com/en/unicode/U+0041#UNC_VERSION) |
| **The Character (Hex) Code**: | **[A (U+0041)](https://www.compart.com/en/unicode/U+0041)** |
| Block: | [Basic Latin, U+0000 - U+007F](https://www.compart.com/en/unicode/block/U+0000) [[3]](https://www.compart.com/en/unicode/U+0041#UNC_BLOCKS) |
| Plane: | [Basic Multilingual Plane, U+0000 - U+FFFF](https://www.compart.com/en/unicode/plane/U+0000) [[3]](https://www.compart.com/en/unicode/U+0041#UNC_BLOCKS) |
| Script: | [Latin](https://www.compart.com/en/unicode/scripts/Latn) (Latn) [[4]](https://www.compart.com/en/unicode/U+0041#UNC_SCRIPTS) |
| Category: | [Uppercase Letter](https://www.compart.com/en/unicode/category/Lu) (Lu) [[1]](https://www.compart.com/en/unicode/U+0041#UNC_DB) |
| Bidirectional Class: | [Left To Right](https://www.compart.com/en/unicode/bidiclass/L) (L) [[1]](https://www.compart.com/en/unicode/U+0041#UNC_DB) |
| Combining Class: | [Not Reordered](https://www.compart.com/en/unicode/combining/0) (0) [[1]](https://www.compart.com/en/unicode/U+0041#UNC_DB) |
| Character is Mirrored: | No [[1]](https://www.compart.com/en/unicode/U+0041#UNC_DB) |
| GCGID: | LA020000 [[5]](https://www.compart.com/en/unicode/U+0041#GCGID) |
| PostScript Name: | A [[6]](https://www.compart.com/en/unicode/U+0041#UNC_PS) |
| HTML Entity: | `&#65;`  
`&#x41;` |
| UTF-8 Encoding: | `0x41` |
| UTF-16 Encoding: | `0x0041` |
| UTF-32 Encoding: | `0x00000041` |
| **Corresponding Lowercase Character** : | [a (U+0061)](https://www.compart.com/en/unicode/U+0061)[[1]](https://www.compart.com/en/unicode/U+0041#UNC_DB) |

### functions ord() & hex()

使用 `ord()` 回傳字元的值（回傳值為十進位）。

```python
hex(ord('A'))
## 輸出：
'0x41'

hex(ord('α'))
## 輸出：
'0x3b1'

```

### int() 使用十六進位

> 說明傳入參數使用的是十六進位

```python
int('3B1', 16) ## 使用十六進位
## 輸出：
945

```

### 使用 escape codes

因為鍵盤的限制，我們有時需要使用 `by hex code` 或 `by name` 來取得特殊字元。

```python
## by hex code
"\u03b1" ## "\u03B1" 也一樣
## 輸出：
'α'

## by name
"\N{Greek Small Letter Alpha}"
## 輸出：
'α'

```

by hex code 有兩種，大 `U` 和小 `u`。

> Note the case difference **\uxxxx** and **\Uxxxxxxxx** for 4 vs 8 digit codes
> 
> 大 `U` 和小 `u` 後方的 A-F，大小寫都可以。

- 大 `U` 8個字元，例如：“\U0001f40d”

- 小 `u` 4個字元，例如：“\u03b1”

- 如果字元小於4個，使用大 `U` 和小 `u` 是一樣的。例如：“\u03b1” & “\U000003b1”。

```python
## 大 `U` 8個字元
"\U0001f40d"
## 輸出： ## "\N{snake}" by name 亦同
'🐍'

## 小 `u` 4個字元
"\u03b1"
## 輸出：
'α'

```

### Unicode 參考資料：

> **[Find all Unicode Characters from Hieroglyphs to Dingbats – Unicode Compart](https://www.compart.com/en/unicode/)**
>
> Unicode-Compart is a site dedicated to Unicode and all things related to Unicode, characters, glyphs and internationalization
