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:
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]:
56for code in map[ord, mystr]:
57
58for code in map[ord, mystr]:
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']:
0for code in mystr.encode['ascii']:
61 Modify the code above to get characters from their corresponding ASCII values using the chr[] function as shown below.for code in map[ord, mystr]:
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'
for code in map[ord, mystr]:
62 for code in map[ord, mystr]:
63for 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 1121
The ASCII value of 'p' is 1122
The ASCII value of 'p' is 1123
for code in mystr.encode['ascii']:
9The ASCII value of 'p' is 1125
The ASCII value of 'p' is 1121
["The ASCII value of 'g' is", 103]8
["The ASCII value of 'g' is", 103]9
The ASCII value of k is 1070
The ASCII value of k is 1071
The ASCII value of k is 1072
# Program to find the ASCII value of the given character
c = 'p'
print["The ASCII value of '" + c + "' is", ord[c]]
8The ASCII value of k is 1074
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 1121
The ASCII value of 'p' is 1122
The ASCII value of 'p' is 1123
for code in mystr.encode['ascii']:
9The ASCII value of 'p' is 1125
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
0The ASCII value of k is 107
1 The ASCII value of k is 107
2
8# Program to find the ASCII value of the given character
c = 'p'
print["The ASCII value of '" + c + "' is", ord[c]]
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;a
Output : 97JavaD
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
The ASCII value of 'p' is 112
1The ASCII value of k is 107
9The ASCII value of A is 65
4The ASCII value of A is 65
5The 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]
- 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]]
9The ASCII value of 'p' is 1120
The ASCII value of 'p' is 1121
The ASCII value of 'p' is 1122
The ASCII value of 'p' is 1123
The ASCII value of 'p' is 1124
The ASCII value of 'p' is 1125
The ASCII value of 'p' is 1121
The ASCII value of 'p' is 1127
# 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']:
8The ASCII value of 'p' is 1129
>>> chr[65] 'A' >>> chr[120] 'x' >>> chr[ord['S'] + 1] 'T'0
The ASCII value of 'p' is 1121
>>> 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]
- 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]]
9The ASCII value of 'p' is 1120
The ASCII value of 'p' is 1121
The ASCII value of 'p' is 1122
The ASCII value of 'p' is 1123
The ASCII value of 'p' is 1124
The ASCII value of 'p' is 1125
The ASCII value of 'p' is 1121
>>> 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 1121
>>> 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]
- 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]]
9The ASCII value of 'p' is 1121
The ASCII value of 'p' is 1122
The ASCII value of 'p' is 1123
The ASCII value of 'p' is 1124
The ASCII value of 'p' is 1125
The ASCII value of 'p' is 1121
>>> 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 659
for code in map[ord, mystr]:
08["The ASCII value of 'g' is", 103]9
for code in map[ord, mystr]:
10for code in map[ord, mystr]:
11 for code in map[ord, mystr]:
12The ASCII value of 'p' is 1121
>>> 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
- 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]:
17The ASCII value of k is 1079
The ASCII value of A is 650
for code in map[ord, mystr]:
20The ASCII value of 'p' is 1120
The ASCII value of 'p' is 1121
The ASCII value of k is 1079
The ASCII value of A is 654
The ASCII value of A is 655
for code in map[ord, mystr]:
26The ASCII value of 'p' is 1121
The ASCII value of 'p' is 1120
The ASCII value of A is 659
The ASCII value of 'p' is 1122
The ASCII value of 'p' is 1123
for code in map[ord, mystr]:
022The ASCII value of A is 659
# 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]:
06The ASCII value of A is 659
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]]
0for code in map[ord, mystr]:
41for code in map[ord, mystr]:
42for code in map[ord, mystr]:
11 for code in map[ord, mystr]:
12The ASCII value of 'p' is 1121
>>> 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']:
8for code in map[ord, mystr]:
50for 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']:
5for code in map[ord, mystr]:
53for 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]:
56for code in map[ord, mystr]:
57for 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']:
0for code in map[ord, mystr]:
61for code in map[ord, mystr]:
62 for code in map[ord, mystr]:
63for code in map[ord, mystr]:
64 for code in map[ord, mystr]:
65The ASCII value of 'p' is 1121
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]]
5for code in map[ord, mystr]:
70The ASCII value of 'p' is 1121
# 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']:
7for code in map[ord, mystr]:
73for code in map[ord, mystr]:
74for code in map[ord, mystr]:
75Input:
for code in map[ord, mystr]:
2Output:
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]