Wonderful use of encode and decode

Source: Internet
Author: User
Tags rfc

During network programming, you often need to print the received data in hexadecimal format for easy viewing. Today we found that there is such a simple method in Python.

 

>>> "Hello". encode ("hex ")
'68656c6c6f'

 

You can also

>>> '68656c6c6f'. Decode ("hex ")
'Hello'

 

I checked the manual and found these codec available.

Codec

Aliases

Operand type

Purpose

Base64_codec Base64, base-64 Byte string Convert operand to mime base64
Bz2_codec Bz2 Byte string Compress the operand using bz2
Hex_codec HEX Byte string Convert operand to hexadecimal representation, with two digits per byte
Idna   Unicode string Implements RFC 3490. New in Version 2.3. See alsoEncodings. Idna
MBCS DBCS Unicode string Windows only: encode operand according to the ANSI codePage (cp_acp)
Palmos   Unicode string Encoding of palmos 3.5
Punycode   Unicode string Implements RFC 3492. New in Version 2.3.
Quopri_codec Quopri, quoted-printable, quotedprintable Byte string Convert operand to mime quoted printable
Raw_unicode_escape   Unicode string Produce a string that is suitable as raw Unicode literal in Python source code
Rot_13 Rot13 Unicode string Returns the CAESAR-Cypher encryption of the operand
String_escape   Byte string Produce a string that is suitable as string literal in Python source code
Undefined   Any Raise an exception for all conversions. can be used as the system encoding if no automatic coercion between byte and Unicode strings is desired.
Unicode_escape   Unicode string Produce a string that is suitable as Unicode literal in Python source code
Unicode_internal   Unicode string Return the internal representation of the operand
Uu_codec UU Byte string Convert the operand Using UUENCODE
Zlib_codec Zip, zlib Byte string Compress the operand using Gzip

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.