Hướng dẫn how does encode and decode work python? - Mã hóa và giải mã hoạt động như thế nào trong python?

Mã hóa chuỗi python ()

Chức năng Python String Encode () được sử dụng để mã hóa chuỗi bằng mã hóa được cung cấp. Hàm này trả về đối tượng byte. Nếu chúng tôi không cung cấp mã hóa, mã hóa UTF-8 được sử dụng làm mặc định.

Python Byte Decode ()

Hàm giải mã byte python () được sử dụng để chuyển đổi byte thành đối tượng chuỗi. Cả hai chức năng này cho phép chúng tôi chỉ định sơ đồ xử lý lỗi để sử dụng cho các lỗi mã hóa/giải mã. Mặc định là ‘nghiêm ngặt có nghĩa là các lỗi mã hóa làm tăng một unicodeEncodeError. Một số giá trị có thể khác là ‘bỏ qua,‘ thay thế và ‘xmlcharrefreplace. Chúng ta hãy xem xét một ví dụ đơn giản về các hàm giải mã () () Chuỗi () Chuỗi ().

str_original = 'Hello'

bytes_encoded = str_original.encode(encoding='utf-8')
print(type(bytes_encoded))

str_decoded = bytes_encoded.decode()
print(type(str_decoded))

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)

Output:



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True

Ví dụ trên không thể hiện rõ ràng việc sử dụng mã hóa. Hãy cùng xem xét một ví dụ khác, nơi chúng tôi sẽ nhận được đầu vào từ người dùng và sau đó mã hóa nó. Chúng tôi sẽ có một số ký tự đặc biệt trong chuỗi đầu vào được nhập bởi người dùng.

str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)

Đầu ra:

Hướng dẫn how does encode and decode work python? - Mã hóa và giải mã hoạt động như thế nào trong python?

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True

Bạn có thể kiểm tra toàn bộ tập lệnh Python và nhiều ví dụ về Python từ Kho lưu trữ GitHub của chúng tôi.

Tham khảo: str.encode () API Doc, byte.decode () API Doc

Cải thiện bài viết

Lưu bài viết

Decode () là một phương thức được chỉ định trong các chuỗi trong Python 2. Phương thức này được sử dụng để chuyển đổi từ một sơ đồ mã hóa, trong đó chuỗi đối số được mã hóa thành sơ đồ mã hóa mong muốn. Điều này hoạt động đối diện với mã hóa. Nó chấp nhận mã hóa của chuỗi mã hóa để giải mã nó và trả về chuỗi gốc.
This method is used to convert from one encoding scheme, in which argument string is encoded to the desired encoding scheme. This works opposite to the encode. It accepts the encoding of the encoding string to decode it and returns the original string.

Cú pháp: Giải mã (mã hóa, lỗi)decode(encoding, error)

Các tham số: Mã hóa: Chỉ định mã hóa trên cơ sở giải mã phải được thực hiện.Error: Quyết định cách xử lý các lỗi nếu chúng xảy ra, ví dụ: ’nghiêm ngặt làm tăng lỗi Unicode trong trường hợp ngoại lệ và‘ bỏ qua các lỗi đã xảy ra.
encoding : Specifies the encoding on the basis of which decoding has to be performed.
error : Decides how to handle the errors if they occur, e.g ‘strict’ raises Unicode error in case of exception and ‘ignore’ ignores the errors occurred.

Trả về: Trả về chuỗi gốc từ chuỗi được mã hóa. Returns the original string from the encoded string.

& nbsp; mã số 1: mã để giải mã chuỗi
Code #1 : Code to decode the string

str = "geeksforgeeks"



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
8


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
9



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6
str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)
1



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7
str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)
4


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
9



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6
str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)
7


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
4
str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)
9
Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
0
Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
1

Output:

The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks

Ứng dụng: Mã hóa và giải mã cùng nhau có thể được sử dụng trong các ứng dụng đơn giản lưu trữ mật khẩu ở phía sau và nhiều ứng dụng khác như mật mã liên quan đến việc giữ bí mật thông tin. Một minh chứng nhỏ của ứng dụng mật khẩu được mô tả dưới đây.
Encoding and decoding together can be used in the simple applications of storing passwords in the back end and many other applications like cryptography which deals with keeping the information confidential.
A small demonstration of the password application is depicted below.

& NBSP; Mã số 2: Mã để chứng minh ứng dụng mã hóa mã hóa
Code #2 : Code to demonstrate application of encode-decode

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
2= "geeksforgeeks"

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
5=
Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
7

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
5=
The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks
0
The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks
1
str_original = input('Please enter string data:\n')

bytes_encoded = str_original.encode()

str_decoded = bytes_encoded.decode()

print('Encoded bytes =', bytes_encoded)
print('Decoded String =', str_decoded)
print('str_original equals str_decoded =', str_original == str_decoded)
9
Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
0____15

The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks
5= "geeksforgeeks"

The encoded string in base64 format is :  Z2Vla3Nmb3JnZWVrcw==

The decoded string is :  geeksforgeeks
8= "geeksforgeeks"



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
3
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
4
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
5

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
7=__

str5



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7str8str9

=0 =1



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7=4str9



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6=7=8str9

"geeksforgeeks"0__

Please enter string data:
aåb∫cçd∂e´´´ƒg©1¡
Encoded bytes = b'a\xc3\xa5b\xe2\x88\xabc\xc3\xa7d\xe2\x88\x82e\xc2\xb4\xc2\xb4\xc2\xb4\xc6\x92g\xc2\xa91\xc2\xa1'
Decoded String = aåb∫cçd∂e´´´ƒg©1¡
str_original equals str_decoded = True
7



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
3
Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
4 "geeksforgeeks"7

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!
6"geeksforgeeks"9=__

str5



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7str8str9

=0



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
03

str5



Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
6


Encoded bytes = b'Hello'
Decoded String = Hello
str_original equals str_decoded = True
7=4str9

Output:

Password entered : geeksforgeeks
Wrong Password!!

Password entered : i_lv_coding
You are logged in!!

Mã hóa và giải mã có nghĩa là gì trong Python?

Trong ngôn ngữ lập trình Python, mã hóa đại diện cho một chuỗi unicode dưới dạng một chuỗi byte.Điều này thường xảy ra khi bạn chuyển một thể hiện qua mạng hoặc lưu nó vào tệp đĩa.Giải mã biến đổi một chuỗi byte thành một chuỗi unicode.encoding represents a Unicode string as a string of bytes. This commonly occurs when you transfer an instance over a network or save it to a disk file. Decoding transforms a string of bytes into a Unicode string.

Làm thế nào để giải mã hoạt động trong Python?

Hàm giải mã byte python () được sử dụng để chuyển đổi byte thành đối tượng chuỗi.Cả hai chức năng này cho phép chúng tôi chỉ định sơ đồ xử lý lỗi để sử dụng cho các lỗi mã hóa/giải mã.Mặc định là 'nghiêm ngặt' có nghĩa là các lỗi mã hóa làm tăng unicodeEncodeError.. Both these functions allow us to specify the error handling scheme to use for encoding/decoding errors. The default is 'strict' meaning that encoding errors raise a UnicodeEncodeError.

Làm thế nào để Python xử lý mã hóa?

Python String Encode () Phương thức Cú pháp:..
Cú pháp: Mã hóa (mã hóa, lỗi).
Parameters:.
Trả về: Trả về chuỗi ở dạng được mã hóa ..

Sự khác biệt của giải mã và mã hóa là gì?

Bộ mã hóa và bộ giải mã là các mạch logic kết hợp.Một trong những khác biệt chính giữa hai thuật ngữ này là bộ mã hóa cho mã nhị phân làm đầu ra trong khi bộ giải mã nhận mã nhị phân.the encoder gives binary code as the output while the decoder receives binary code.