Hướng dẫn how do you code special characters in python? - làm thế nào để bạn mã các ký tự đặc biệt trong python?

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Unicode in Python: Làm việc với mã hóa ký tự This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Unicode in Python: Working With Character Encodings

Show

Xử lý mã hóa ký tự trong Python hoặc bất kỳ ngôn ngữ nào khác có thể có vẻ đau đớn. Những nơi như Stack Overflow có hàng ngàn câu hỏi xuất phát từ sự nhầm lẫn về các ngoại lệ như

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
4 và
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
5. Hướng dẫn này được thiết kế để xóa sương mù
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
6 và minh họa rằng làm việc với dữ liệu văn bản và nhị phân trong Python 3 có thể là một trải nghiệm suôn sẻ. Hỗ trợ của Python sườn Unicode rất mạnh mẽ và mạnh mẽ, nhưng phải mất một thời gian để làm chủ.

Hướng dẫn này là khác nhau bởi vì nó không phải là ngôn ngữ-bất khả tri mà thay vào đó là cố tình trung tâm. Bạn vẫn sẽ nhận được một đoạn mồi không liên tục ngôn ngữ, nhưng sau đó bạn sẽ đi sâu vào các hình minh họa trong Python, với các đoạn văn nặng về văn bản được giữ ở mức tối thiểu. Bạn có thể thấy cách sử dụng các khái niệm mã hóa ký tự trong mã Python trực tiếp.

Đến cuối hướng dẫn này, bạn sẽ:

  • Nhận tổng quan về khái niệm về mã hóa ký tự và hệ thống đánh số
  • Hiểu cách mã hóa xuất hiện với Python từ
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7 và
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    8
  • Biết về sự hỗ trợ trong Python cho các hệ thống đánh số thông qua các hình thức khác nhau của nó
  • Làm quen với các chức năng tích hợp Python, liên quan đến mã hóa ký tự và hệ thống đánh số

Các hệ thống mã hóa và đánh số ký tự được kết nối chặt chẽ đến mức chúng cần được đề cập trong cùng một hướng dẫn hoặc nếu không thì việc điều trị của một trong hai sẽ hoàn toàn không đủ.

Những gì một nhân vật mã hóa?

Có hàng chục nếu không phải hàng trăm mã hóa nhân vật. Cách tốt nhất để bắt đầu hiểu những gì chúng là bao gồm một trong những mã hóa nhân vật đơn giản nhất, ASCII.

Cho dù bạn tự học hay có nền tảng khoa học máy tính chính thức, rất có thể bạn đã thấy một bảng ASCII một hoặc hai lần. ASCII là một nơi tốt để bắt đầu tìm hiểu về mã hóa ký tự vì nó là một mã hóa nhỏ và có chứa. (Quá nhỏ, hóa ra.)

Nó bao gồm những điều sau đây:

  • Chữ cái chữ thường chữ cái: a đến z: a through z
  • Chữ hoa chữ hoa: a đến z: A through Z
  • Một số dấu câu và biểu tượng:
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    0 và
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    1, để đặt tên cho một cặp vợ chồng
    :
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    0 and
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    1, to name a couple
  • Các ký tự khoảng trắng: Một không gian thực tế (
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    2), cũng như một dòng mới, trả về vận chuyển, tab ngang, tab dọc và một vài cái khác
    : an actual space (
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    2), as well as a newline, carriage return, horizontal tab, vertical tab, and a few others
  • Một số ký tự không thể in: các ký tự như Backspace,
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    3, có thể được in theo nghĩa đen theo cách mà chữ A có thể
    : characters such as backspace,
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    3, that can’t be printed literally in the way that the letter A can

Vậy một định nghĩa chính thức hơn về mã hóa ký tự là gì?

Ở cấp độ rất cao, nó có một cách dịch các ký tự (như chữ cái, dấu câu, ký hiệu, khoảng trắng và ký tự điều khiển) sang số nguyên và cuối cùng là các bit. Mỗi ký tự có thể được mã hóa thành một chuỗi các bit duy nhất. Đừng lo lắng nếu bạn run rẩy về khái niệm bit, bởi vì chúng tôi sẽ sớm nhận được chúng.

Các loại khác nhau được phác thảo đại diện cho các nhóm ký tự. Mỗi ký tự duy nhất có một điểm mã tương ứng, mà bạn có thể nghĩ là chỉ là một số nguyên. Các ký tự được phân đoạn thành các phạm vi khác nhau trong bảng ASCII:code point, which you can think of as just an integer. Characters are segmented into different ranges within the ASCII table:

Phạm vi điểm mãLớp
0 đến 31Ký tự điều khiển/không in
32 đến 64Dấu câu, biểu tượng, số và không gian
65 đến 90Chữ cái chữ cái chữ hoa
91 đến 96Các biểu đồ bổ sung, chẳng hạn như
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
4 và
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
5
97 đến 122Chữ cái chữ cái chữ thường chữ cái
123 đến 126Các biểu đồ bổ sung, chẳng hạn như
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
6 và
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
7
127 Kiểm soát/ký tự không thể in (
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
8)

Toàn bộ bảng ASCII chứa 128 ký tự. Bảng này ghi lại bộ ký tự hoàn chỉnh mà ASCII cho phép. Nếu bạn không thấy một nhân vật ở đây, thì bạn chỉ cần thể hiện nó dưới dạng văn bản in theo sơ đồ mã hóa ASCII.character set that ASCII permits. If you don’t see a character here, then you simply can’t express it as printed text under the ASCII encoding scheme.

Điểm mãTên nhân vật)Điểm mãTên nhân vật)
0 Nul (null)64
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
9
1 Soh (bắt đầu tiêu đề)65
>>> n_bits_required(110)
7
0
2 STX (bắt đầu văn bản)66
>>> n_bits_required(110)
7
1
3 ETX (kết thúc văn bản)67
>>> n_bits_required(110)
7
2
4 EOT (kết thúc truyền tải)68
>>> n_bits_required(110)
7
3
5 ENQ (Yêu cầu)69
>>> n_bits_required(110)
7
4
6 ACK (xác nhận)70
>>> n_bits_required(110)
7
5
7 Bel (Bell)71
>>> n_bits_required(110)
7
6
8 BS (Backspace)72
>>> n_bits_required(110)
7
7
9 HT (tab ngang)73
>>> n_bits_required(110)
7
8
10 LF (nguồn cấp dữ liệu)74
>>> n_bits_required(110)
7
9
11 VT (tab dọc)75
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
0
12 FF (nguồn cấp dữ liệu mẫu)76
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
1
13 Cr (Quay trở lại)77
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
2
14 Vì vậy (dịch chuyển ra)78
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
3
15 Si (Shift In)79
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
4
16 DLE (Escape liên kết dữ liệu)80
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
5
17 DC1 (Điều khiển thiết bị 1)81
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
6
18 DC2 (Điều khiển thiết bị 2)82
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
7
19 DC3 (Điều khiển thiết bị 3)83
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
8
20 DC4 (Điều khiển thiết bị 4)84
>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128
9
21 NAK (xác nhận tiêu cực)85
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
0
22 Syn (nhàn rỗi đồng bộ)86
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
1
23 ETB (Kết thúc khối truyền)87
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
2
24 Có thể (hủy)88
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
3
25 Em (kết thúc trung bình)89
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
4
26 Phụ (thay thế)90
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
5
27 ESC (Escape)91
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
4
28 FS (Bộ phân cách tệp)92
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
5
29 GS (phân tách nhóm)93
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
8
30 RS (Bộ phân cách ghi)94
>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'
9
31 Hoa Kỳ (phân tách đơn vị)95
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
0
32 SP (không gian)96
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
1
33
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
2
97
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
3
34
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
4
98
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
5
35
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
6
99
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
7
36
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
8
100
>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17
9
37
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
0
101
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
1
38
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
2
102
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
3
39
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
4
103
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
5
40
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
6
104
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
7
41
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
8
105
>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17
9
42
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
00
106
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
01
43
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
02
107
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
03
44
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
04
108
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
05
45
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
06
109
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
07
46
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
08
110
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
09
47
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
10
111
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
11
48
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
12
112
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
13
49
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
14
113
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
15
50
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
16
114
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
17
51
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
18
115
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
19
52
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
20
116
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
21
53
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
22
117
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
23
54
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
24
118
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
25
55
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
26
119
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
27
56
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
28
120
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
29
57
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
30
121
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
31
58
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
32
122
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
33
59
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
34
123
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
6
60
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
36
124
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
7
61
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
38
125
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
39
62
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
40
126
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
41
63
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
42
127 Del (xóa)

Mô -đun >>> import string >>> s = "What's wrong with ASCII?!?!?" >>> s.rstrip(string.punctuation) 'What's wrong with ASCII' 43

Mô-đun Python sườn

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
43 là một cửa một cửa thuận tiện cho các hằng số chuỗi rơi vào bộ ký tự ASCII.

Ở đây, cốt lõi của mô -đun trong tất cả vinh quang của nó:

# From lib/python3.7/string.py

whitespace = ' \t\n\r\v\f'
ascii_lowercase = 'abcdefghijklmnopqrstuvwxyz'
ascii_uppercase = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ'
ascii_letters = ascii_lowercase + ascii_uppercase
digits = '0123456789'
hexdigits = digits + 'abcdef' + 'ABCDEF'
octdigits = '01234567'
punctuation = r"""!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~"""
printable = digits + ascii_letters + punctuation + whitespace

Hầu hết các hằng số này nên tự ghi chép trong tên định danh của chúng. Chúng tôi sẽ bao gồm những gì

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
45 và
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
46 trong thời gian ngắn.

Bạn có thể sử dụng các hằng số này để thao tác chuỗi hàng ngày:

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'

Một chút bồi dưỡng

Bây giờ là thời điểm tốt cho việc bồi dưỡng ngắn về bit, đơn vị thông tin cơ bản nhất mà máy tính biết.bit, the most fundamental unit of information that a computer knows.

Một chút là một tín hiệu chỉ có hai trạng thái có thể. Có nhiều cách khác nhau để thể hiện một chút một chút mà tất cả đều có nghĩa là cùng một điều:

  • 0 hoặc 1
  • "có hay không"
  • >>> import string
    
    >>> s = "What's wrong with ASCII?!?!?"
    >>> s.rstrip(string.punctuation)
    'What's wrong with ASCII'
    
    47 hoặc
    >>> import string
    
    >>> s = "What's wrong with ASCII?!?!?"
    >>> s.rstrip(string.punctuation)
    'What's wrong with ASCII'
    
    48
  • "bật hoặc tắt"

Bảng ASCII của chúng tôi từ phần trước sử dụng những gì bạn và tôi sẽ gọi các số (0 đến 127), nhưng những gì được gọi chính xác hơn là các số trong cơ sở 10 (thập phân).

Bạn cũng có thể thể hiện từng số cơ sở này với một chuỗi bit (cơ sở 2). Dưới đây là các phiên bản nhị phân từ 0 đến 10 trong thập phân:

Số thập phânNhị phân (nhỏ gọn)Nhị phân (dạng đệm)
0 0 00000000
1 1 00000001
2 10 00000010
3 11 00000011
4 100 00000100
5 101 00000101
6 110 00000110
7 111 00000111
8 1000 00001000
9 1001 00001001
10 1010 00001010

Lưu ý rằng khi số thập phân N tăng, bạn cần các bit quan trọng hơn để thể hiện ký tự được thiết lập và bao gồm cả số đó.significant bits to represent the character set up to and including that number.

Ở đây, một cách tiện dụng để đại diện cho các chuỗi ASCII như các chuỗi bit trong Python. Mỗi ký tự từ chuỗi ASCII được mã hóa giả thành 8 bit, với khoảng trắng ở giữa các chuỗi 8 bit mà mỗi người đại diện cho một ký tự duy nhất:

>>>

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'

Một chút bồi dưỡng

  • Bây giờ là thời điểm tốt cho việc bồi dưỡng ngắn về bit, đơn vị thông tin cơ bản nhất mà máy tính biết.

  • Một chút là một tín hiệu chỉ có hai trạng thái có thể. Có nhiều cách khác nhau để thể hiện một chút một chút mà tất cả đều có nghĩa là cùng một điều:

0 hoặc 1

"có hay không"

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
47 hoặc
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
48

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits

"bật hoặc tắt"

  • Bảng ASCII của chúng tôi từ phần trước sử dụng những gì bạn và tôi sẽ gọi các số (0 đến 127), nhưng những gì được gọi chính xác hơn là các số trong cơ sở 10 (thập phân).
  • Bạn cũng có thể thể hiện từng số cơ sở này với một chuỗi bit (cơ sở 2). Dưới đây là các phiên bản nhị phân từ 0 đến 10 trong thập phân:
  • Số thập phân

Nhị phân (nhỏ gọn)

Nhị phân (dạng đệm)

>>>

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8

Một chút bồi dưỡng

>>>

>>> n_bits_required(110)
7

Tất cả điều này phục vụ để chứng minh một khái niệm: ASCII, nói đúng, một mã 7 bit. Bảng ASCII mà bạn đã thấy ở trên chứa 128 điểm và ký tự mã, bao gồm từ 0 đến 127. Điều này đòi hỏi 7 bit:

>>>

>>> n_bits_required(128)  # 0 through 127
7
>>> n_possible_values(7)
128

Vấn đề với điều này là các máy tính hiện đại don don lưu trữ nhiều thứ trong các khe 7 bit. Họ lưu lượng truy cập theo đơn vị 8 bit, được gọi là một byte.byte.

Điều này có nghĩa là không gian lưu trữ được ASCII sử dụng là nửa trống. Nếu nó không rõ lý do tại sao điều này, hãy nghĩ lại bảng thập phân đến nhị phân từ trên cao. Bạn có thể diễn đạt số 0 và 1 chỉ với 1 bit hoặc bạn có thể sử dụng 8 bit để diễn đạt chúng lần lượt là 00000000 và 00000001.

Bạn có thể biểu thị các số từ 0 đến 3 chỉ với 2 bit hoặc 00 đến 11 hoặc bạn có thể sử dụng 8 bit để diễn đạt chúng dưới dạng 00000000, 00000001, 00000010 và 00000011, tương ứng. Điểm mã ASCII cao nhất, 127, chỉ yêu cầu 7 bit đáng kể.

Biết được điều này, bạn có thể thấy rằng

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
57 chuyển đổi chuỗi ASCII thành đại diện
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 của byte, trong đó mọi nhân vật tiêu thụ một byte:

>>>

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

ASCII, việc sử dụng không đúng cách các byte 8 bit được cung cấp bởi các máy tính hiện đại đã dẫn đến một gia đình mã hóa không chính thức, mâu thuẫn mà mỗi ký tự bổ sung được chỉ định sẽ được sử dụng với 128 điểm mã có sẵn còn lại được phép trong sơ đồ mã hóa ký tự 8 bit.

Không chỉ các mã hóa khác nhau này đụng độ với nhau, mà mỗi người trong số chúng vẫn là một đại diện không hoàn chỉnh của các nhân vật thế giới, bất kể thực tế là họ đã sử dụng thêm một bit.

Trong những năm qua, một nhân vật mã hóa Mega-Scheme đã đến để cai trị tất cả. Tuy nhiên, trước khi chúng tôi đến đó, hãy để nói chuyện trong một phút về các hệ thống đánh số, đây là một nền tảng cơ bản của các sơ đồ mã hóa ký tự.

Bao gồm tất cả các cơ sở: các hệ thống số khác

Trong cuộc thảo luận về ASCII ở trên, bạn đã thấy rằng mỗi ký tự ánh xạ tới một số nguyên trong phạm vi từ 0 đến 127.

Phạm vi số này được thể hiện trong thập phân (cơ sở 10). Đó là cách mà bạn, tôi và những người còn lại của chúng ta đã quen với việc đếm, không có lý do phức tạp hơn chúng ta có 10 ngón tay.

Nhưng có những hệ thống đánh số khác cũng đặc biệt phổ biến trong suốt mã nguồn CPython. Mặc dù số cơ bản của người Viking là giống nhau, nhưng tất cả các hệ thống đánh số chỉ là những cách khác nhau để thể hiện cùng một số.

Nếu tôi hỏi bạn số nào chuỗi

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
59 đại diện cho, bạn sẽ đúng khi cho tôi một cái nhìn kỳ lạ trước khi trả lời rằng nó đại diện cho mười một.

Tuy nhiên, biểu diễn chuỗi này có thể diễn đạt các số cơ bản khác nhau trong các hệ thống đánh số khác nhau. Ngoài thập phân, các lựa chọn thay thế bao gồm các hệ thống đánh số phổ biến sau:

  • Nhị phân: cơ sở 2: base 2
  • Octal: Cơ sở 8: base 8
  • Hexadecimal (Hex): Cơ sở 16: base 16

Nhưng chúng ta nói rằng, trong một hệ thống đánh số nhất định, các con số được thể hiện trong cơ sở n?

Đây là cách tốt nhất mà tôi biết để nói rõ điều này có nghĩa là gì: đó là số lượng ngón tay mà bạn đã tính đến trong hệ thống đó.

Nếu bạn muốn có một giới thiệu đầy đủ hơn nhưng vẫn nhẹ nhàng về các hệ thống đánh số, mã Charles Petzold, là một cuốn sách cực kỳ tuyệt vời khám phá các nền tảng của mã máy tính một cách chi tiết.

Một cách để chứng minh làm thế nào các hệ thống đánh số khác nhau diễn giải cùng một điều với hàm tạo Python từ

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
60. Nếu bạn chuyển
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 đến
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
60, Python sẽ giả định theo mặc định rằng chuỗi thể hiện một số trong cơ sở 10 trừ khi bạn nói với nó khác:

>>>

>>> int('11')
11
>>> int('11', base=10)  # 10 is already default
11
>>> int('11', base=2)  # Binary
3
>>> int('11', base=8)  # Octal
9
>>> int('11', base=16)  # Hex
17

Có một cách phổ biến hơn để nói với Python rằng số nguyên của bạn được gõ vào một cơ sở khác 10. Python chấp nhận các dạng theo nghĩa đen của mỗi trong số 3 hệ thống đánh số thay thế ở trên:literal forms of each of the 3 alternative numbering systems above:

Loại nghĩa đenTiếp đầu ngữThí dụ
n/an/a
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
63
Nhị phân nhị phân
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
64 hoặc
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
65
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
66
Nghĩa đen của bát phân
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
67 hoặc
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
68
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
69
Hex theo nghĩa đen
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
70 hoặc
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
71
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
72

Tất cả đều là các dạng phụ của các chữ số nguyên. Bạn có thể thấy rằng chúng tạo ra các kết quả tương tự, tương ứng, khi các cuộc gọi đến

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
60 với các giá trị không mặc định
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
74. Tất cả họ chỉ là
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
9 cho Python:integer literals. You can see that these produce the same results, respectively, as the calls to
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
60 with non-default
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
74 values. They’re all just
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
9 to Python:

>>>

>>> 11
11
>>> 0b11  # Binary literal
3
>>> 0o11  # Octal literal
9
>>> 0x11  # Hex literal
17

Ở đây, cách bạn có thể nhập các loại tương đương nhị phân, bát phân và thập lục phân của các số thập phân từ 0 đến 20. Bất kỳ loại nào hoàn toàn có giá trị trong shell thông dịch viên Python hoặc mã nguồn và tất cả đều hoạt động để loại

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
9:

Số thập phânNhị phânBát phânHex
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
12
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
78
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
79
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
80
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
14
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
82
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
83
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
84
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
16
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
86
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
87
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
88
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
18
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
66
Nghĩa đen của bát phân
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
92
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
20
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
94
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
95
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
96
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
22
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
98
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
99
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
00
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
24
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
02
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
03
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
04
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
26
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
06
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
07
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
08
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
28
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
10
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
11
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
12
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
30
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
14
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
69
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
16
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
17
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
18
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
19
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
20
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
63
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
22
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
23
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
24
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
25
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
26
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
27
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
28
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
29
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
30
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
31
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
32
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
33
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
34
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
35
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
36
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
37
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
38
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
39
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
40
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
41
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
42
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
43
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
44
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
45
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
46
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
47
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
72
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
49
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
50
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
51
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
52
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
53
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
54
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
55
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
56
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
57
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
58
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
59
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
60

Thật tuyệt vời khi các biểu thức này phổ biến như thế nào trong thư viện tiêu chuẩn Python. Nếu bạn muốn tự mình nhìn thấy, hãy điều hướng đến bất cứ nơi nào thư mục

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
61 của bạn nằm và kiểm tra việc sử dụng các nghĩa đen như thế này:

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
0

Điều này sẽ hoạt động trên bất kỳ hệ thống Unix nào có

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
62. Bạn có thể sử dụng
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
63 để tìm kiếm nghĩa đen hoặc chữ \ b0b, để tìm kiếm văn bản nhị phân.

Những gì đối số cho việc sử dụng các cú pháp theo nghĩa đen

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
9 này? Nói tóm lại, đó là vì vì 2, 8 và 16 đều là sức mạnh của 2, trong khi 10 thì không. Ba hệ thống số thay thế này thỉnh thoảng cung cấp một cách để thể hiện các giá trị theo cách thân thiện với máy tính. Ví dụ, số 65536 hoặc 216, chỉ là 10000 trong thập lục phân, hoặc
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
65 dưới dạng một nghĩa đen hình lục giác.

Nhập Unicode

Như bạn đã thấy, vấn đề với ASCII là nó gần như không phải là một bộ nhân vật đủ lớn để phù hợp với bộ ngôn ngữ, phương ngữ, biểu tượng và glyphs của thế giới. (Nó thậm chí không đủ lớn cho một mình tiếng Anh.)

Unicode về cơ bản phục vụ mục đích tương tự như ASCII, nhưng nó chỉ bao gồm một cách, cách, cách tập hợp các điểm mã lớn hơn. Có một số ít các mã hóa xuất hiện theo trình tự thời gian giữa ASCII và Unicode, nhưng chúng không thực sự đáng được đề cập đến vì Unicode và một trong những sơ đồ mã hóa của nó, UTF-8, đã được sử dụng chủ yếu.

Hãy nghĩ về Unicode như một phiên bản lớn của bảng ASCII, một người có 1.114.112 điểm mã có thể. Đó là 0 đến 1.114.111, hoặc 0 đến 17 * (216) - 1 hoặc

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
66 Hexadecimal. Trong thực tế, ASCII là một tập hợp con hoàn hảo của Unicode. 128 ký tự đầu tiên trong bảng Unicode tương ứng chính xác với các ký tự ASCII mà bạn đã mong đợi một cách hợp lý.

Vì lợi ích của việc chính xác về mặt kỹ thuật, bản thân Unicode không phải là một mã hóa. Thay vào đó, Unicode được triển khai bởi các mã hóa ký tự khác nhau mà bạn sẽ thấy sớm. Unicode được coi là bản đồ tốt hơn (một cái gì đó như

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
67) hoặc bảng cơ sở dữ liệu 2 cột. Nó ánh xạ các ký tự (như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
68,
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
69 hoặc thậm chí
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
70) thành các số nguyên dương, khác biệt. Một mã hóa nhân vật cần cung cấp thêm một chút.Unicode itself is not an encoding. Rather, Unicode is implemented by different character encodings, which you’ll see soon. Unicode is better thought of as a map (something like a
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
67) or a 2-column database table. It maps characters (like
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
68,
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
69, or even
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
70) to distinct, positive integers. A character encoding needs to offer a bit more.

Unicode chứa hầu như mọi nhân vật mà bạn có thể tưởng tượng, bao gồm cả những người không thể in bổ sung. Một trong những mục yêu thích của tôi là dấu hiệu từ phải sang trái, có mã điểm 8207 và được sử dụng trong văn bản với cả các tập lệnh ngôn ngữ từ trái sang phải và từ phải, chẳng hạn như một bài viết có chứa cả đoạn văn tiếng Anh và tiếng Ả Rập .

Unicode vs UTF-8

Nó đã không mất nhiều thời gian để mọi người nhận ra rằng tất cả các nhân vật trên thế giới không thể được đóng gói thành một byte. Nó rõ ràng từ đó rằng các mã hóa hiện đại, toàn diện hơn sẽ cần sử dụng nhiều byte để mã hóa một số ký tự.

Bạn cũng thấy ở trên rằng Unicode không phải là một mã hóa ký tự toàn diện. Tại sao vậy?

Có một điều mà Unicode không nói với bạn: Nó không cho bạn biết làm thế nào để có được các bit thực tế từ văn bản chỉ các điểm mã. Nó không cho bạn biết đủ về cách chuyển đổi văn bản thành dữ liệu nhị phân và ngược lại.

Unicode là một tiêu chuẩn mã hóa trừu tượng, không phải là mã hóa. Đó là nơi mà UTF-8 và các chương trình mã hóa khác xuất hiện. Tiêu chuẩn Unicode (Bản đồ các ký tự cho các điểm mã) xác định một số mã hóa khác nhau từ bộ ký tự đơn của nó.

UTF-8 cũng như anh em họ ít sử dụng hơn, UTF-16 và UTF-32, là các định dạng mã hóa để đại diện cho các ký tự Unicode dưới dạng dữ liệu nhị phân của một hoặc nhiều byte trên mỗi ký tự. Chúng tôi sẽ thảo luận về UTF-16 và UTF-32 trong một khoảnh khắc, nhưng UTF-8 đã chiếm phần chia sẻ lớn nhất của chiếc bánh.

Điều đó đưa chúng ta đến một định nghĩa đã quá hạn lâu. Nó có nghĩa là gì, chính thức, để mã hóa và giải mã?encode and decode?

Mã hóa và giải mã trong Python 3

Loại Python 3

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 có nghĩa là đại diện cho văn bản có thể đọc được của con người và có thể chứa bất kỳ ký tự Unicode nào.

Loại

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8, ngược lại, đại diện cho dữ liệu nhị phân hoặc chuỗi byte thô, bản chất không có mã hóa được gắn vào nó.

Mã hóa và giải mã là quá trình đi từ cái này sang cái khác:

Hướng dẫn how do you code special characters in python? - làm thế nào để bạn mã các ký tự đặc biệt trong python?
Mã hóa so với giải mã (Hình ảnh: Python thật)

Trong

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
73 và
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
74, tham số
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
75 là
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
76 theo mặc định, mặc dù nó thường an toàn hơn và không rõ ràng hơn để chỉ định nó:

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
1

Kết quả của

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
77 là một đối tượng
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8. Cả hai byte theo nghĩa đen (như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
79) và các biểu diễn của byte chỉ cho phép các ký tự ASCII.

Đây là lý do tại sao, khi gọi

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
80, tương thích ASCII
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
81 được phép được biểu diễn như nó là, nhưng N với Tilde được thoát đến
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
82. Trình tự trông lộn xộn đó đại diện cho hai byte,
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
83 và
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
84 trong hex:

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
2

Kết quả của

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
77 là một đối tượng
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8. Cả hai byte theo nghĩa đen (như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
79) và các biểu diễn của byte chỉ cho phép các ký tự ASCII.

Đây là lý do tại sao, khi gọi >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 80, tương thích ASCII >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 81 được phép được biểu diễn như nó là, nhưng N với Tilde được thoát đến >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 82. Trình tự trông lộn xộn đó đại diện cho hai byte, >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 83 và >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 84 trong hex:

Đó là, ký tự

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
85 yêu cầu hai byte cho biểu diễn nhị phân của nó theo UTF-8.

  • Python 3: All-in on unicode

  • Python 3 là tất cả trên Unicode và UTF-8 cụ thể. Ở đây, điều đó có nghĩa là gì:

  • Mã nguồn Python 3 được coi là UTF-8 theo mặc định. Điều này có nghĩa là bạn không cần

    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    86 ở đầu các tệp
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    87 trong Python 3.

  • Tất cả văn bản (

    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7) là Unicode theo mặc định. Văn bản unicode được mã hóa được biểu diễn dưới dạng dữ liệu nhị phân (
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    8). Loại
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7 có thể chứa bất kỳ ký tự unicode theo nghĩa đen nào, chẳng hạn như
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    91, tất cả chúng sẽ được lưu trữ dưới dạng Unicode.

  • Python 3 chấp nhận nhiều điểm mã Unicode trong các định danh, có nghĩa là

    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    92 là hợp lệ nếu điều này tạo ra sự ưa thích của bạn.

Mô -đun Python từ

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
93 mặc định là cờ
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
94 thay vì
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
95. Điều này có nghĩa là, ví dụ,
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
96 phù hợp với các ký tự từ Unicode, không chỉ các chữ cái ASCII.

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
3

Kết quả của

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
77 là một đối tượng
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8. Cả hai byte theo nghĩa đen (như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
79) và các biểu diễn của byte chỉ cho phép các ký tự ASCII.

Đây là lý do tại sao, khi gọi >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 80, tương thích ASCII >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 81 được phép được biểu diễn như nó là, nhưng N với Tilde được thoát đến >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 82. Trình tự trông lộn xộn đó đại diện cho hai byte, >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 83 và >>> def make_bitseq(s: str) -> str: ... if not s.isascii(): ... raise ValueError("ASCII only allowed") ... return " ".join(f"{ord(i):08b}" for i in s) >>> make_bitseq("bits") '01100010 01101001 01110100 01110011' >>> make_bitseq("CAPS") '01000011 01000001 01010000 01010011' >>> make_bitseq("$25.43") '00100100 00110010 00110101 00101110 00110100 00110011' >>> make_bitseq("~5") '01111110 00110101' 84 trong hex:

Đó là, ký tự

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
85 yêu cầu hai byte cho biểu diễn nhị phân của nó theo UTF-8.variable-length encoding. It’s tempting to gloss over what this means, but it’s worth delving into.

Python 3: All-in on unicode

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
4

Kết quả của

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
77 là một đối tượng
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8. Cả hai byte theo nghĩa đen (như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
79) và các biểu diễn của byte chỉ cho phép các ký tự ASCII.

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
5

Kết quả của

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
77 là một đối tượng
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8. Cả hai byte theo nghĩa đen (như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
79) và các biểu diễn của byte chỉ cho phép các ký tự ASCII.

  • Đây là lý do tại sao, khi gọi
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    80, tương thích ASCII
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    81 được phép được biểu diễn như nó là, nhưng N với Tilde được thoát đến
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    82. Trình tự trông lộn xộn đó đại diện cho hai byte,
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    83 và
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    84 trong hex:
  • Đó là, ký tự
    >>> def make_bitseq(s: str) -> str:
    ...     if not s.isascii():
    ...         raise ValueError("ASCII only allowed")
    ...     return " ".join(f"{ord(i):08b}" for i in s)
    
    >>> make_bitseq("bits")
    '01100010 01101001 01110100 01110011'
    
    >>> make_bitseq("CAPS")
    '01000011 01000001 01010000 01010011'
    
    >>> make_bitseq("$25.43")
    '00100100 00110010 00110101 00101110 00110100 00110011'
    
    >>> make_bitseq("~5")
    '01111110 00110101'
    
    85 yêu cầu hai byte cho biểu diễn nhị phân của nó theo UTF-8.

Python 3: All-in on unicode

Python 3 là tất cả trên Unicode và UTF-8 cụ thể. Ở đây, điều đó có nghĩa là gì:Mã nguồn Python 3 được coi là UTF-8 theo mặc định. Điều này có nghĩa là bạn không cần
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
86 ở đầu các tệp
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
87 trong Python 3.
Tất cả văn bản (
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7) là Unicode theo mặc định. Văn bản unicode được mã hóa được biểu diễn dưới dạng dữ liệu nhị phân (
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8). Loại
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 có thể chứa bất kỳ ký tự unicode theo nghĩa đen nào, chẳng hạn như
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
91, tất cả chúng sẽ được lưu trữ dưới dạng Unicode.
Python 3 chấp nhận nhiều điểm mã Unicode trong các định danh, có nghĩa là
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
92 là hợp lệ nếu điều này tạo ra sự ưa thích của bạn.
Mô -đun Python từ
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
93 mặc định là cờ
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
94 thay vì
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
95. Điều này có nghĩa là, ví dụ,
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
96 phù hợp với các ký tự từ Unicode, không chỉ các chữ cái ASCII.
Mặc định
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
75 trong
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
77 và
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
99 là UTF-8.
Có một thuộc tính khác có nhiều sắc thái hơn, đó là mặc định
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
75 cho
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
01 tích hợp phụ thuộc vào nền tảng và phụ thuộc vào giá trị của
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
02:
Một lần nữa, bài học ở đây là cẩn thận về việc đưa ra các giả định khi nói đến tính phổ quát của UTF-8, ngay cả khi đó là mã hóa chiếm ưu thế. Không bao giờ đau khi được rõ ràng trong mã của bạn.
Một byte, hai byte, ba byte, bốnMột tính năng quan trọng là UTF-8 là một mã hóa có độ dài thay đổi. Nó rất hấp dẫn để che giấu ý nghĩa của điều này, nhưng nó đáng để đi sâu vào.Nghĩ lại phần trên ASCII. Tất cả mọi thứ trong nhu cầu đất đai mở rộng nhiều nhất là một byte không gian. Bạn có thể nhanh chóng chứng minh điều này với biểu thức máy phát sau:UTF-8 khá khác nhau. Một ký tự unicode nhất định có thể chiếm bất cứ nơi nào từ một đến bốn byte. Ở đây, một ví dụ về một ký tự unicode duy nhất chiếm bốn byte:
Đây là một tính năng tinh tế nhưng quan trọng của
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
03:
Độ dài của một ký tự unicode duy nhất là Python
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 sẽ luôn luôn là 1, bất kể nó chiếm bao nhiêu byte.
Độ dài của cùng một ký tự được mã hóa thành
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8 sẽ ở bất cứ đâu trong khoảng từ 1 đến 4.
Bảng dưới đây tóm tắt những loại ký tự chung nào phù hợp với mỗi nhóm có độ dài byte:
Phạm vi thập phânPhạm vi hexNhững gì bao gồmVí dụ

0 đến 127
**A huge array of languages and symbols—mostly Chinese, Japanese, and Korean by volume (also ASCII and Latin alphabets)
***Additional Chinese, Japanese, Korean, and Vietnamese characters, plus more symbols and emojis

def n_possible_values(nbits: int) -> int: return 2 ** nbits 06 đến def n_possible_values(nbits: int) -> int: return 2 ** nbits 07

Hoa Kỳ ASCII

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
08,
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
09,
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
10,
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
11

>>>

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
6

128 đến 2047

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
12 đến
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
13

Hầu hết các bảng chữ cái Latin*

Mã hóaByte trên mỗi ký tự (bao gồm)Chiều dài thay đổi
UTF-81 đến 4Đúng
UTF-162 đến 4Đúng
UTF-164 2 đến 4

UTF-32

Không

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
7

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

>>>

Lý do cho điều này là các điểm mã trong phạm vi

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
32 đến
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
33 (2048 đến 65535 trong thập phân) chiếm ba byte trong UTF-8 so với chỉ hai trong UTF-16.

Tôi không phải là bất kỳ phương tiện nào khuyến nghị bạn nhảy lên tàu UTF-16, bất kể bạn có hoạt động bằng ngôn ngữ mà các nhân vật thường nằm trong phạm vi này hay không. Trong số các lý do khác, một trong những lập luận mạnh mẽ cho việc sử dụng UTF-8 là, trong thế giới mã hóa, đó là một ý tưởng tuyệt vời để hòa nhập với đám đông.

Chưa kể, đó là năm 2019: Bộ nhớ máy tính rẻ, vì vậy hãy tiết kiệm 4 byte bằng cách cố gắng sử dụng UTF-16 được cho là không xứng đáng.

Chức năng tích hợp của Python

  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    34
  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    35
  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    36
  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    37
  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    38
  • >>> import string
    
    >>> s = "What's wrong with ASCII?!?!?"
    >>> s.rstrip(string.punctuation)
    'What's wrong with ASCII'
    
    60
  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    40
  • >>> import string
    
    >>> s = "What's wrong with ASCII?!?!?"
    >>> s.rstrip(string.punctuation)
    'What's wrong with ASCII'
    
    51
  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    42

Bạn đã thực hiện nó thông qua phần khó khăn. Thời gian để sử dụng những gì bạn đã thấy cho đến nay trong Python.

  • Python có một nhóm các chức năng tích hợp liên quan theo một cách nào đó để đánh số hệ thống và mã hóa ký tự: are for obtaining a different representation of an input. Each one produces a

    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7. The first,
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    34, produces an ASCII only representation of an object, with non-ASCII characters escaped. The remaining three give binary, hexadecimal, and octal representations of an integer, respectively. These are only representations, not a fundamental change in the input.

  • Chúng có thể được nhóm lại một cách hợp lý dựa trên mục đích của chúng: are class constructors for their respective types,

    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    8,
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7, and
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    9. They each offer ways of coercing the input into the desired type. For instance, as you saw earlier, while
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    55 is probably more common, you might also see
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    56.

  • def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    34,
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    35,
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    38 và
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    40 là để có được một biểu diễn khác của đầu vào. Mỗi người tạo ra một
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7. Đầu tiên,
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    34, tạo ra một đại diện chỉ ASCII của một đối tượng, với các ký tự không phải ASCII đã thoát ra. Ba phần còn lại cho các biểu diễn nhị phân, thập lục phân và phân bổ của một số nguyên, tương ứng. Đây chỉ là những đại diện, không phải là một thay đổi cơ bản trong đầu vào.
    are inverses of each other in that the Python
    >>> import string
    
    >>> s = "What's wrong with ASCII?!?!?"
    >>> s.rstrip(string.punctuation)
    'What's wrong with ASCII'
    
    51 function converts a
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    7 character to its base-10 code point, while
    def n_possible_values(nbits: int) -> int:
        return 2 ** nbits
    
    37 does the opposite.

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
36,
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
42 và
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
60 là các hàm tạo lớp cho các loại tương ứng của chúng,
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8,
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 và
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
9. Mỗi người cung cấp các cách ép buộc đầu vào vào loại mong muốn. Chẳng hạn, như bạn đã thấy trước đó, trong khi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
55 có lẽ là phổ biến hơn, bạn cũng có thể thấy
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
56.

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
51 và
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
37 là nghịch đảo của nhau ở chỗ hàm Python
>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
51 chuyển đổi ký tự
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 thành điểm mã cơ sở-10 của nó, trong khi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
37 thực hiện ngược lại.
Ở đây, một cái nhìn chi tiết hơn về mỗi chín chức năng này:Hàm sốChữ kýChấp nhận
Loại trở lạiMục đích
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
34
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
ASCII chỉ đại diện cho một đối tượng, với các ký tự không phải ASCII đã thoát ra
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
35
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
71

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
72

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
73

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
74

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
34
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
ASCII chỉ đại diện cho một đối tượng, với các ký tự không phải ASCII đã thoát ra
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
79

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
80

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
81

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
ASCII chỉ đại diện cho một đối tượng, với các ký tự không phải ASCII đã thoát ra
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
35
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
89

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
90

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
34
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
ASCII chỉ đại diện cho một đối tượng, với các ký tự không phải ASCII đã thoát ra
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
35
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
ASCII chỉ đại diện cho một đối tượng, với các ký tự không phải ASCII đã thoát ra
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
00

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
01

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
04

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
05

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
34
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
63
Thay đổi

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7

ASCII chỉ đại diện cho một đối tượng, với các ký tự không phải ASCII đã thoát ra

Không

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
8

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> import string

>>> s = "What's wrong with ASCII?!?!?"
>>> s.rstrip(string.punctuation)
'What's wrong with ASCII'
9

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
0

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
1

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
2

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
3

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
4

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
5

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

>>>

Không

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
6

Một khía cạnh gây tò mò khác của gia đình UTF là UTF-8 sẽ không phải lúc nào cũng chiếm ít không gian hơn UTF-16. Điều đó có vẻ phản trực giác về mặt toán học, nhưng nó hoàn toàn có thể:

>>>

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
7

Bên cạnh việc đặt các ký tự Unicode thực tế, không được phân loại vào bảng điều khiển, có những cách khác để nhập các chuỗi Unicode.

Một trong những phần dày đặc nhất của tài liệu Python, là phần về phân tích từ vựng, cụ thể là phần về chuỗi và byte. Cá nhân, tôi đã phải đọc phần này về một, hai, hoặc có thể chín lần để nó thực sự chìm vào.

Một phần của những gì nó nói là có tối đa sáu cách mà Python sẽ cho phép bạn nhập cùng một ký tự Unicode.

Cách đầu tiên và phổ biến nhất là gõ chính nhân vật theo nghĩa đen, như bạn đã thấy. Phần khó khăn với phương pháp này là tìm các tổ hợp phím thực tế. Đó là nơi mà các phương pháp khác để nhận và đại diện cho các nhân vật phát huy tác dụng. Ở đây, danh sách đầy đủ:

Trình tự thoátNghĩaCách thể hiện
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
68
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
24
Ký tự có giá trị bát phân
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
25
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
26
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
27
Nhân vật có giá trị hex
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
28
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
29
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
30
Ký tự có tên
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
31 trong cơ sở dữ liệu Unicode
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
32
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
33
Ký tự với giá trị hex 16 bit (2 byte)
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
34
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
35
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
36
Ký tự với giá trị hex 32 bit (4 byte)
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
37
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
38

Ở đây, một số bằng chứng và xác nhận của những điều trên:

>>>

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
8

Bây giờ, có hai cảnh báo chính:

  1. Không phải tất cả các hình thức này hoạt động cho tất cả các ký tự. Đại diện Hex của số nguyên 300 là

    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    39, đơn giản là sẽ phù hợp với mã thoát 2-hex-chữ số
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    27. Điểm mã cao nhất mà bạn có thể siết chặt vào chuỗi thoát này là
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    41 (
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    42). Tương tự đối với
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    24, nó sẽ chỉ hoạt động lên đến
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    44 (
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    45).

  2. Đối với

    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    46,
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    47 và
    >>> from math import ceil, log
    
    >>> def n_bits_required(nvalues: int) -> int:
    ...     return ceil(log(nvalues) / log(2))
    
    >>> n_bits_required(256)
    8
    
    48, chính xác là nhiều chữ số được yêu cầu như được hiển thị trong các ví dụ này. Điều này có thể ném bạn cho một vòng lặp vì cách các bảng Unicode thường hiển thị các mã cho các ký tự, với số lượng ký tự hex hàng đầu và biến đổi. Điều quan trọng là các bảng Unicode thường không có các mã này.

Chẳng hạn, nếu bạn tham khảo unicode-table.com để biết thông tin về chữ gothic faihu (hoặc FEHU),

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
50, bạn sẽ thấy rằng nó được liệt kê là có mã
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
51.

Làm thế nào để bạn đặt điều này vào

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
33 hoặc
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
36? Chà, bạn có thể phù hợp với nó trong
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
33 bởi vì nó là một nhân vật 4 byte và sử dụng
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
36 để thể hiện nhân vật này, bạn sẽ cần phải bỏ qua trình tự:

Điều này cũng có nghĩa là mẫu

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
36 là chuỗi thoát duy nhất có khả năng giữ bất kỳ ký tự unicode nào.

Các mã hóa khác có sẵn trong Python

Cho đến nay, bạn đã thấy bốn mã hóa nhân vật:

  1. Ascii
  2. UTF-8
  3. UTF-16
  4. UTF-32

Có rất nhiều người khác ngoài kia.

Một ví dụ là Latin-1 (còn được gọi là ISO-8859-1), về mặt kỹ thuật là mặc định cho giao thức chuyển siêu văn bản (HTTP), mỗi RFC 2616. Windows có biến thể Latin-1 riêng được gọi là CP1252.

Danh sách đầy đủ các mã hóa được chấp nhận được chôn vùi trong tài liệu cho mô -đun

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
57, là một phần của thư viện tiêu chuẩn Python.

Có một mã hóa được công nhận hữu ích hơn để nhận thức được, đó là

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
58. Nếu bạn có một
def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
7 được giải mã và muốn nhanh chóng nhận được một đại diện cho chữ Unicode đã thoát ra của nó, thì bạn có thể chỉ định mã hóa này trong
>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
73:

>>>

>>> def make_bitseq(s: str) -> str:
...     if not s.isascii():
...         raise ValueError("ASCII only allowed")
...     return " ".join(f"{ord(i):08b}" for i in s)

>>> make_bitseq("bits")
'01100010 01101001 01110100 01110011'

>>> make_bitseq("CAPS")
'01000011 01000001 01010000 01010011'

>>> make_bitseq("$25.43")
'00100100 00110010 00110101 00101110 00110100 00110011'

>>> make_bitseq("~5")
'01111110 00110101'
9

Bạn biết những gì họ nói về các giả định

Chỉ vì Python đưa ra giả định mã hóa UTF-8 cho các tệp và mã mà bạn tạo ra không có nghĩa là bạn, lập trình viên, nên hoạt động với cùng một giả định về dữ liệu bên ngoài.

Hãy để nói điều đó một lần nữa bởi vì nó là một quy tắc để sống bằng cách: khi bạn nhận được dữ liệu nhị phân (byte) từ nguồn bên thứ ba, cho dù đó là từ một tệp hoặc qua mạng, thực tế tốt nhất là kiểm tra xem dữ liệu chỉ định mã hóa. Nếu nó không có, thì nó sẽ hỏi bạn.

Tất cả I/O xảy ra trong byte, không phải văn bản và byte chỉ là những cái và số không cho máy tính cho đến khi bạn nói với nó bằng cách thông báo về mã hóa.

Ở đây, một ví dụ về nơi mọi thứ có thể đi sai. Bạn đã đăng ký API gửi cho bạn một công thức trong ngày mà bạn nhận được trong

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
8 và luôn được giải mã bằng cách sử dụng
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
62 mà không có vấn đề gì. Vào ngày đặc biệt này, một phần của công thức trông như thế này:

>>>

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
0

Có vẻ như công thức gọi một số bột, nhưng chúng tôi không biết bao nhiêu:

>>>

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
1

Uh oh. Có một số người mắc bệnh

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
4 có thể cắn bạn khi bạn đưa ra các giả định về mã hóa. Bạn kiểm tra với máy chủ API. Lo và kìa, dữ liệu thực sự được gửi qua được mã hóa trong Latin-1:

>>>

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
2

Chúng tôi đi ở đó. Trong Latin-1, mọi nhân vật đều phù hợp với một byte duy nhất, trong khi ký tự ¼ ¼ chiếm hai byte trong UTF-8 (

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
64).

Bài học ở đây là nó có thể nguy hiểm khi cho rằng việc mã hóa bất kỳ dữ liệu nào được gửi cho bạn. Nó thường là UTF-8 trong những ngày này, nhưng nó có tỷ lệ nhỏ trong các trường hợp mà nó không phải là điều đó sẽ thổi bay mọi thứ.

Nếu bạn thực sự cần phải từ bỏ tàu và đoán mã hóa, thì hãy xem thư viện

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
65, sử dụng phương pháp từ Mozilla để đưa ra một phỏng đoán có giáo dục về văn bản được mã hóa mơ hồ. Điều đó nói rằng, một công cụ như
>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
65 nên là phương sách cuối cùng của bạn, không phải đầu tiên của bạn.

Tỷ lệ cược và kết thúc: >>> from math import ceil, log >>> def n_bits_required(nvalues: int) -> int: ... return ceil(log(nvalues) / log(2)) >>> n_bits_required(256) 8 67

Chúng tôi sẽ không đề cập đến

>>> from math import ceil, log

>>> def n_bits_required(nvalues: int) -> int:
...     return ceil(log(nvalues) / log(2))

>>> n_bits_required(256)
8
67 từ Thư viện tiêu chuẩn Python, cho phép bạn tương tác và tra cứu trên cơ sở dữ liệu ký tự Unicode (UCD):

>>>

def n_possible_values(nbits: int) -> int:
    return 2 ** nbits
3

Gói lên

Trong bài viết này, bạn đã giải mã chủ đề rộng rãi và áp đặt mã hóa nhân vật trong Python.

Bạn đã bao phủ rất nhiều mặt đất ở đây:

  • Các khái niệm cơ bản của mã hóa ký tự và hệ thống đánh số
  • Số nguyên, nhị phân, bát phân, hex, str và byte theo nghĩa đen trong Python
  • Các chức năng tích hợp của Python, liên quan đến hệ thống mã hóa và đánh số ký tự
  • Python 3 Xử lý văn bản so với dữ liệu nhị phân

Bây giờ, đi ra và mã hóa!

Tài nguyên

Để biết thêm chi tiết về các chủ đề được đề cập ở đây, hãy xem các tài nguyên này:

  • Joel Spolsky: Tối thiểu tuyệt đối mỗi nhà phát triển phần mềm hoàn toàn, tích cực phải biết về các bộ unicode và ký tự (không có lý do!) The Absolute Minimum Every Software Developer Absolutely, Positively Must Know About Unicode and Character Sets (No Excuses!)
  • David Zentgraf: Điều mà mọi lập trình hoàn toàn cần biết về bộ mã hóa và bộ nhân vật để làm việc với văn bản What every programmer absolutely, positively needs to know about encodings and character sets to work with text
  • Mozilla: Một cách tiếp cận tổng hợp để phát hiện ngôn ngữ/mã hóa A composite approach to language/encoding detection
  • Wikipedia: UTF-8 UTF-8
  • John Skeet: Unicode và .net Unicode and .NET
  • Charles Petzold: Mã: Ngôn ngữ ẩn của phần cứng và phần mềm máy tính Code: The Hidden Language of Computer Hardware and Software
  • Nhóm làm việc mạng, RFC 3629: UTF-8, Định dạng chuyển đổi của ISO 10646 UTF-8, a transformation format of ISO 10646
  • Tiêu chuẩn kỹ thuật Unicode #18: Các biểu thức chính quy Unicode Unicode Regular Expressions

Các tài liệu Python có hai trang về chủ đề:

  • Những gì mới trong Python 3.0
  • Unicode Howto

Xem bây giờ hướng dẫn này có một khóa học video liên quan được tạo bởi nhóm Python thực sự. Xem nó cùng với hướng dẫn bằng văn bản để làm sâu sắc thêm sự hiểu biết của bạn: Unicode in Python: Làm việc với mã hóa ký tự This tutorial has a related video course created by the Real Python team. Watch it together with the written tutorial to deepen your understanding: Unicode in Python: Working With Character Encodings

Làm thế nào để bạn in các biểu tượng đặc biệt trong Python?

Để in bất kỳ ký tự nào trong trình thông dịch Python, hãy sử dụng \ u để biểu thị một ký tự unicode và sau đó làm theo mã ký tự.Chẳng hạn, mã cho β là 03B2, do đó để in lệnh được in ('\ u03b2').use a \u to denote a unicode character and then follow with the character code. For instance, the code for β is 03B2, so to print β the command is print('\u03B2') .

Làm thế nào để bạn có được các ký tự đặc biệt trong một chuỗi trong Python?

Chúng ta có thể sử dụng các hàm re.match (), re.Search (), re.sub () để phát hiện các ký tự đặc biệt từ chuỗi.Tất cả các chức năng được đề cập ở trên đều thuộc về mô-đun Regex là gói tích hợp trong Python.re. match(), re.search(), re. sub() functions for detecting the special characters from the string. The above-mentioned functions all belong to RegEx module which is a built-in package in Python.

Các biến Python có thể có ký tự đặc biệt không?

Một tên biến chỉ có thể chứa các ký tự alpha-numeric và nhấn mạnh (A-Z, 0-9 và _) (A-z, 0-9, and _ )

Làm thế nào để bạn viết các ký tự Unicode trong Python?

Để bao gồm các ký tự Unicode trong mã nguồn Python của bạn, bạn có thể sử dụng các ký tự Escape Unicode trong Mẫu \ U0123 trong chuỗi của bạn.Trong Python 2. X, bạn cũng cần tiền tố chuỗi theo nghĩa đen với 'u'.use Unicode escape characters in the form \u0123 in your string. In Python 2. x, you also need to prefix the string literal with 'u'.