Hướng dẫn convert char to ascii python - chuyển đổi char sang ascii python

Câu trả lời được chấp nhận là chính xác, nhưng có một cách thông minh/hiệu quả hơn để làm điều này nếu bạn cần chuyển đổi cả một loạt các ký tự ASCII sang mã ASCII của họ cùng một lúc. Thay vì làm:

Show
for ch in mystr:
    code = ord(ch)

hoặc nhanh hơn một chút:

for code in map(ord, mystr):

Bạn chuyển đổi sang các loại gốc Python lặp lại mã trực tiếp. Trên Python 3, nó tầm thường:

for code in mystr.encode('ascii'):

Và trên Python 2.6/2.7, nó chỉ liên quan nhiều hơn một chút vì nó không có kiểu PY3

for code in map(ord, mystr):
4 (
for code in map(ord, mystr):
4 là bí danh đối với
for code in map(ord, mystr):
6, được lặp đi lặp lại bởi nhân vật), nhưng chúng có
for code in map(ord, mystr):
7:

# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):

Mã hóa dưới dạng một loại tự lặp lại theo thứ tự có nghĩa là chuyển đổi đi nhanh hơn nhiều; Trong các thử nghiệm cục bộ trên cả PY2.7 và PY3.5, việc lặp lại

for code in map(ord, mystr):
6 để lấy mã ASCII của nó bằng cách sử dụng
for code in map(ord, mystr):
9 bắt đầu mất khoảng hai lần trong
for code in mystr.encode('ascii'):
0 10
for code in map(ord, mystr):
6 so với sử dụng
for code in mystr.encode('ascii'):
2 trên PY2 hoặc
for code in mystr.encode('ascii'):
3 Dài hơn, hệ số nhân được trả cho
for code in map(ord, mystr):
9 tăng lên ~ 6,5x-7x.

Nhược điểm duy nhất là việc chuyển đổi là tất cả cùng một lúc, vì vậy kết quả đầu tiên của bạn có thể mất nhiều thời gian hơn một chút và một

for code in map(ord, mystr):
6 thực sự khổng lồ sẽ có một tỷ lệ lớn tạm thời có khả năng quan trọng.

Trong chương trình này, bạn sẽ học cách tìm giá trị ASCII của một ký tự và hiển thị nó.

Để hiểu ví dụ này, bạn nên có kiến ​​thức về các chủ đề lập trình Python sau:

  • Đầu vào và đầu ra Python Basic
  • Lập trình Python Chức năng tích hợp

ASCII là viết tắt của mã tiêu chuẩn Mỹ để trao đổi thông tin.

Đó là một giá trị số được cung cấp cho các ký tự và ký hiệu khác nhau, cho các máy tính lưu trữ và thao tác. Ví dụ: giá trị ASCII của chữ

for code in mystr.encode('ascii'):
9 là 65.

Mã nguồn

# Program to find the ASCII value of the given character

c = 'p'
print("The ASCII value of '" + c + "' is", ord(c))

Đầu ra

The ASCII value of 'p' is 112

Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1) To test this program for other characters, change the character assigned to the

# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):
0 variable.

Dưới đây là một phương pháp để in giá trị ASCII của các ký tự trong chuỗi bằng Python:

for code in map(ord, mystr):
54
# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):
5
for code in map(ord, mystr):
56
for code in map(ord, mystr):
57

for code in map(ord, mystr):
58
# If mystr is definitely str, not unicode
for code in bytearray(mystr):

# If mystr could be either str or unicode
for code in bytearray(mystr, 'ascii'):
5
for code in mystr.encode('ascii'):
0
for code in map(ord, mystr):
61
Modify the code above to get characters from their corresponding ASCII values using the chr() function as shown below.

>>> chr(65)
'A'
>>> chr(120)
'x'
>>> chr(ord('S') + 1)
'T'

for code in map(ord, mystr):
62
for code in map(ord, mystr):
63
for code in map(ord, mystr):
64
for code in map(ord, mystr):
65

CPP

>>> chr(65)
'A'
>>> chr(120)
'x'
>>> chr(ord('S') + 1)
'T'
6
>>> chr(65)
'A'
>>> chr(120)
'x'
>>> chr(ord('S') + 1)
'T'
7
>>> chr(65)
'A'
>>> chr(120)
'x'
>>> chr(ord('S') + 1)
'T'
8

  • The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    2
    The ASCII value of 'p' is 112
    
    3
    for code in mystr.encode('ascii'):
    
    9
    The ASCII value of 'p' is 112
    
    5
  • The ASCII value of 'p' is 112
    
    1
    ("The ASCII value of 'g' is", 103)
    8
    ("The ASCII value of 'g' is", 103)
    9
    The ASCII value of k is 107
    0
    The ASCII value of k is 107
    1
    The ASCII value of k is 107
    2
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    The ASCII value of k is 107
    4
  • CPP

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    6
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    7
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    8

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    2
    The ASCII value of 'p' is 112
    
    3
    for code in mystr.encode('ascii'):
    
    9
    The ASCII value of 'p' is 112
    
    5
    Hướng dẫn convert char to ascii python - chuyển đổi char sang ascii python

    The ASCII value of 'p' is 112
    
    1
    ("The ASCII value of 'g' is", 103)
    8
    ("The ASCII value of 'g' is", 103)
    9
    The ASCII value of k is 107
    0
    The ASCII value of k is 107
    1
    The ASCII value of k is 107
    2
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    The ASCII value of k is 107
    4

    Mã Java: Ở đây, để tìm giá trị ASCII của C, chúng tôi chỉ gán C cho một biến ASCII INT. Trong nội bộ, Java chuyển đổi giá trị ký tự thành giá trị ASCII. & NBSP;
    Output : 97

    JavaD
    Output : 68

    The ASCII value of k is 107
    9
    The ASCII value of A is 65
    0
    The ASCII value of A is 65
    1

    1. The ASCII value of 'p' is 112
      
      1
      The ASCII value of k is 107
      9
      The ASCII value of A is 65
      4
      The ASCII value of A is 65
      5
      The ASCII value of A is 65
      6
      ord() : It converts the given string of length one, returns an integer representing the Unicode code point of the character. For example, ord(‘a’) returns the integer 97. 

    Python

    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    4
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    5
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    6

    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    7
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    8
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    9
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    0
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    4
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    0
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    3__

    Đầu ra

    ("The ASCII value of 'g' is", 103)

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)
    Auxiliary Space: O(1)

    1. Mã C: Chúng tôi sử dụng định dạng xác định định dạng ở đây để cung cấp giá trị số của ký tự. Ở đây %D được sử dụng để chuyển đổi ký tự thành giá trị ASCII của nó. & NBSP;We use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value. 

    C

    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    7

    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    9

    The ASCII value of 'p' is 112
    
    0

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    2
    The ASCII value of 'p' is 112
    
    3
    The ASCII value of 'p' is 112
    
    4
    The ASCII value of 'p' is 112
    
    5

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    7
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    8
    The ASCII value of 'p' is 112
    
    9
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    0

    The ASCII value of 'p' is 112
    
    1
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    2
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    3

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    4

    Đầu ra

    The ASCII value of k is 107

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)

    1. Mã C: Chúng tôi sử dụng định dạng xác định định dạng ở đây để cung cấp giá trị số của ký tự. Ở đây %D được sử dụng để chuyển đổi ký tự thành giá trị ASCII của nó. & NBSP;Here int() is used to convert a character to its ASCII value. 

    C

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    5

    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    9

    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    9

    The ASCII value of 'p' is 112
    
    0

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    2
    The ASCII value of 'p' is 112
    
    3
    The ASCII value of 'p' is 112
    
    4
    The ASCII value of 'p' is 112
    
    5

    The ASCII value of 'p' is 112
    
    1
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    2
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    3

    The ASCII value of 'p' is 112
    
    1
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    2
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    3

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    4

    Đầu ra

    The ASCII value of A is 65

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)O(1)
    Auxiliary Space: O(1)

    1. Mã C: Chúng tôi sử dụng định dạng xác định định dạng ở đây để cung cấp giá trị số của ký tự. Ở đây %D được sử dụng để chuyển đổi ký tự thành giá trị ASCII của nó. & NBSP; Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, Java converts the character value to an ASCII value. 

    C

    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    9

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    2
    The ASCII value of 'p' is 112
    
    3
    The ASCII value of 'p' is 112
    
    4
    The ASCII value of 'p' is 112
    
    5

    The ASCII value of 'p' is 112
    
    1
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    2
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    3

    Mã C ++: Ở đây int () được sử dụng để chuyển đổi một ký tự thành giá trị ASCII của nó. & NBSP;

    CPP

    The ASCII value of A is 65
    9
    for code in map(ord, mystr):
    
    08
    ("The ASCII value of 'g' is", 103)
    9
    for code in map(ord, mystr):
    
    10
    for code in map(ord, mystr):
    
    11
    for code in map(ord, mystr):
    
    12

    The ASCII value of 'p' is 112
    
    1
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    4

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    4

    Đầu ra

    for code in map(ord, mystr):
    
    0

    Độ phức tạp về thời gian: O (1) // Vì không sử dụng vòng lặp không, thuật toán chiếm thời gian không đổi để thực hiện không gian hoạt động: O(1) // since no loop is used the algorithm takes up constant time to perform the operations
    Auxiliary Space: O(1) // since no extra array is used so the space taken by the algorithm is constant

    1. Mã C#: Ở đây, để tìm giá trị ASCII của C, chúng tôi chỉ gán C cho một biến ASCII INT. Trong nội bộ, C# chuyển đổi giá trị ký tự thành giá trị ASCII. & NBSP; Here, to find the ASCII value of c, we just assign c to an int variable ascii. Internally, C# converts the character value to an ASCII value. 

    Csharp

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    6
    for code in map(ord, mystr):
    
    17

    The ASCII value of k is 107
    9
    The ASCII value of A is 65
    0
    for code in map(ord, mystr):
    
    20

    The ASCII value of 'p' is 112
    
    0

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of k is 107
    9
    The ASCII value of A is 65
    4
    The ASCII value of A is 65
    5
    for code in map(ord, mystr):
    
    26

    The ASCII value of 'p' is 112
    
    1
    The ASCII value of 'p' is 112
    
    0

    The ASCII value of A is 65
    9
    The ASCII value of 'p' is 112
    
    2
    The ASCII value of 'p' is 112
    
    3
    for code in map(ord, mystr):
    
    022

    The ASCII value of A is 65
    9
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    8
    for code in map(ord, mystr):
    
    06

    The ASCII value of A is 65
    9
    for code in map(ord, mystr):
    
    38
    ("The ASCII value of 'g' is", 103)
    9
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    0

    for code in map(ord, mystr):
    
    41
    for code in map(ord, mystr):
    
    42
    for code in map(ord, mystr):
    
    11
    for code in map(ord, mystr):
    
    12

    The ASCII value of 'p' is 112
    
    1
    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    4

    >>> chr(65)
    'A'
    >>> chr(120)
    'x'
    >>> chr(ord('S') + 1)
    'T'
    
    4

    Đầu ra

    for code in map(ord, mystr):
    
    0

    Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)
    Auxiliary Space: O(1)

    Dưới đây là một phương pháp để in giá trị ASCII của các ký tự trong chuỗi bằng Python:

    Python3

    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    7
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    8
    for code in map(ord, mystr):
    
    50
    for code in map(ord, mystr):
    
    51
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    5
    for code in map(ord, mystr):
    
    53

    for code in map(ord, mystr):
    
    54
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    5
    for code in map(ord, mystr):
    
    56
    for code in map(ord, mystr):
    
    57

    for code in map(ord, mystr):
    
    58
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    5
    for code in mystr.encode('ascii'):
    
    0
    for code in map(ord, mystr):
    
    61

    for code in map(ord, mystr):
    
    62
    for code in map(ord, mystr):
    
    63
    for code in map(ord, mystr):
    
    64
    for code in map(ord, mystr):
    
    65

    The ASCII value of 'p' is 112
    
    1
    for code in map(ord, mystr):
    
    67
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    5
    # Program to find the ASCII value of the given character
    
    c = 'p'
    print("The ASCII value of '" + c + "' is", ord(c))
    
    5
    for code in map(ord, mystr):
    
    70

    The ASCII value of 'p' is 112
    
    1
    # If mystr is definitely str, not unicode
    for code in bytearray(mystr):
    
    # If mystr could be either str or unicode
    for code in bytearray(mystr, 'ascii'):
    
    7
    for code in map(ord, mystr):
    
    73
    for code in map(ord, mystr):
    
    74
    for code in map(ord, mystr):
    
    75

    Input:

    for code in map(ord, mystr):
    
    2

    Output:

    for code in map(ord, mystr):
    
    3

    Độ phức tạp về thời gian: O (n), trong đó n là độ dài của chuỗi đầu vào.AUXILIARY Không gian: O (1)O(N), where N is the length of the input string.
    Auxiliary Space: O(1)