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, đầu ra và nhập khẩu của Python
- 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ữ The ASCII value of 'p' is 112 4 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 112Lưu ý: Để kiểm tra chương trình này & nbsp; cho các ký tự khác, hãy thay đổi ký tự được gán thành biến The ASCII value of 'p' is 112 5. To test this program for other characters, change the character assigned to the The ASCII value of 'p' is 112 5 variable.
Ở đây chúng tôi đã sử dụng hàm The ASCII value of 'p' is 112 6 để chuyển đổi ký tự thành số nguyên (giá trị ASCII). Hàm này trả về điểm mã Unicode của ký tự đó.
Unicode cũng là một kỹ thuật mã hóa cung cấp một số duy nhất cho một ký tự. Mặc dù ASCII chỉ mã hóa 128 ký tự, Unicode hiện tại có hơn 100.000 ký tự từ hàng trăm tập lệnh.
Lần lượt của bạn: Sửa đổi mã ở trên để lấy các ký tự từ các giá trị ASCII tương ứng của chúng bằng hàm chr () như hiển thị bên dưới. 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'Ở đây, The ASCII value of 'p' is 112 6 và The ASCII value of 'p' is 112 8 là các chức năng tích hợp. Truy cập ở đây để biết thêm về các chức năng tích hợp trong Python.
The ASCII value of A is 659for code in mystr.encode('ascii'): 2 for code in mystr.encode('ascii'): 3The ASCII value of 'p' is 112 022
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ó đối tượng kiểu PY3 The ASCII value of 'p' is 112 9 (The ASCII value of 'p' is 112 9 là bí danh đối với >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 1, được lặp đi lặp lại bởi nhân vật), nhưng chúng có >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 2:
# 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 >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 1 để lấy mã ASCII của nó bằng cách sử dụng >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 4 bắt đầu mất khoảng hai lần trong >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 5 10 >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 1 so với sử dụng ____ Dài hơn, hệ số nhân được trả cho >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 4 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 >>> chr(65) 'A' >>> chr(120) 'x' >>> chr(ord('S') + 1) 'T' 1 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.
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Đưa ra một ký tự, chúng ta cần in giá trị ASCII của nó bằng C/C ++/Java/Python.a
Output : 97Ví dụ:D
Output : 68
Đầu vào: A & NBSP; đầu ra: 97
- Đầu vào: Doutput: 68 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.
Dưới đây là một số phương thức trong các ngôn ngữ lập trình khác nhau để in giá trị ASCII của một ký tự nhất định: & nbsp;
Mã Python bằng hàm Ord: Ord (): Nó chuyển đổi chuỗi đã cho có độ dài một, trả về một số nguyên đại diện cho điểm mã unicode của ký tự. Ví dụ: ord (‘a,) trả về số nguyên 97. & nbsp;
Python
for ch in mystr: code = ord(ch) 4for ch in mystr: code = ord(ch) 5 for ch in mystr: code = ord(ch) 6
("The ASCII value of 'g' is", 103)for ch in mystr:
code = ord(ch)
7for ch in mystr:
code = ord(ch)
8for ch in mystr:
code = ord(ch)
9 for code in map(ord, mystr):
0 for ch in mystr:
code = ord(ch)
4for code in map(ord, mystr):
0 for code in map(ord, mystr):
3__
Auxiliary Space: O(1)
- Đầu raWe use format specifier here to give numeric value of character. Here %d is used to convert character to its ASCII value.
Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (1)
for code in map(ord, mystr): 7
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;
for code in mystr.encode('ascii'): 0
C
for code in map(ord, mystr): 8 for code in map(ord, mystr): 9
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 2 for code in mystr.encode('ascii'): 3for code in mystr.encode('ascii'): 4for code in mystr.encode('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'): 4
for ch in mystr: code = ord(ch) 4for ch in mystr: code = ord(ch) 5 for ch in mystr: code = ord(ch) 6
The ASCII value of k is 107for ch in mystr:
code = ord(ch)
7for ch in mystr:
code = ord(ch)
8for ch in mystr:
code = ord(ch)
9 for code in map(ord, mystr):
0 for ch in mystr:
code = ord(ch)
4for code in map(ord, mystr):
0 for code in map(ord, mystr):
3__O(1)
Auxiliary Space: O(1)
- Đầu raHere int() is used to convert a character to its ASCII value.
Độ phức tạp về thời gian: O (1) Không gian phụ trợ: O (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'): 5
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;
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;
for code in mystr.encode('ascii'): 0
C
for code in map(ord, mystr): 8 for code in map(ord, mystr): 9
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 2 for code in mystr.encode('ascii'): 3for code in mystr.encode('ascii'): 4for code in mystr.encode('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'): 4
for ch in mystr: code = ord(ch) 4for ch in mystr: code = ord(ch) 5 for ch in mystr: code = ord(ch) 6
The ASCII value of A is 65for ch in mystr:
code = ord(ch)
7for ch in mystr:
code = ord(ch)
8for ch in mystr:
code = ord(ch)
9 for code in map(ord, mystr):
0 for ch in mystr:
code = ord(ch)
4for code in map(ord, mystr):
0 for code in map(ord, mystr):
3__O(1)
Auxiliary Space: O(1)
- Đầu ra 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.
Độ phức tạp về thời gian: O (1) Không gian phụ trợ: 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;
C
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 0
for code in map(ord, mystr): 8 for code in map(ord, mystr): 9
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 2 for code in mystr.encode('ascii'): 3for code in mystr.encode('ascii'): 4for code in mystr.encode('ascii'): 5
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 7for ch in mystr: code = ord(ch) 8for code in mystr.encode('ascii'): 9# 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
for code in mystr.encode('ascii'): 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'): 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'): 4
for ch in mystr: code = ord(ch) 4for ch in mystr: code = ord(ch) 5 for ch in mystr: code = ord(ch) 6
The ASCII value of 'p' is 112 0for ch in mystr:
code = ord(ch)
7for ch in mystr:
code = ord(ch)
8for ch in mystr:
code = ord(ch)
9 for code in map(ord, mystr):
0 for ch in mystr:
code = ord(ch)
4for code in map(ord, mystr):
0 for code in map(ord, mystr):
3__: 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
- Đầu ra 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.
Độ phức tạp về thời gian: O (1) Không gian phụ trợ: 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;
C
for code in map(ord, mystr): 8 for code in map(ord, mystr): 9
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 2 for code in mystr.encode('ascii'): 3for code in mystr.encode('ascii'): 4for code in mystr.encode('ascii'): 5
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 0
for code in map(ord, mystr): 8 for code in map(ord, mystr): 9
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 2 for code in mystr.encode('ascii'): 3for code in mystr.encode('ascii'): 4for code in mystr.encode('ascii'): 5
for code in mystr.encode('ascii'): 1for code in mystr.encode('ascii'): 7for ch in mystr: code = ord(ch) 8for code in mystr.encode('ascii'): 9# 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
for code in mystr.encode('ascii'): 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'): 2 # 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'): 3
for code in mystr.encode('ascii'): 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'): 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'): 4
for ch in mystr: code = ord(ch) 4for ch in mystr: code = ord(ch) 5 for ch in mystr: code = ord(ch) 6
The ASCII value of 'p' is 112 0for ch in mystr:
code = ord(ch)
7for ch in mystr:
code = ord(ch)
8for ch in mystr:
code = ord(ch)
9 for code in map(ord, mystr):
0 for ch in mystr:
code = ord(ch)
4for code in map(ord, mystr):
0 for code in map(ord, mystr):
3__
Auxiliary Space: O(1)
Đầu ra
Python3
for ch in mystr: code = ord(ch) 7for ch in mystr: code = ord(ch) 8The ASCII value of 'p' is 112 50The ASCII value of 'p' is 112 51for ch in mystr: code = ord(ch) 5The ASCII value of 'p' is 112 53
Độ phức tạp về thời gian: O (1) Không gian phụ trợ: 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;
C
for code in map(ord, mystr): 8 for code in map(ord, mystr): 9
for code in mystr.encode('ascii'): 1for ch in mystr: code = ord(ch) 7The ASCII value of 'p' is 112 73The ASCII value of 'p' is 112 74The ASCII value of 'p' is 112 75
Input:
The ASCII value of 'p' is 112 2Output:
The ASCII value of 'p' is 112 3for code in mystr.encode('ascii'):
1for code in mystr.encode('ascii'):
2 for code in mystr.encode('ascii'):
3for code in mystr.encode('ascii'):
4for code in mystr.encode('ascii'):
5O(N), where N is the
length of the input string.
Auxiliary Space: O(1)