Python 3 Error resolution using the Binascii method

Source: Internet
Author: User

Environment is Python 3

Questions:

Using the Binascii method has been error typeerror:a Bytes-like object is required, not ' str '

# Coding:utf-8 Import  'worker'=print(b)#b = Binascii.b2a_hex (a)#typeerror:a Bytes-like object is required, not ' str '
Workaround:

Later changes to the following code will not be error

b = Binascii.b2a_hex (A.encode ())
Principle:

In the new version of Python3, the Unicode type is removed, instead it is a string type (str) that uses Unicode characters, the string type (str) becomes the underlying type as shown below, and the encoding becomes the byte type (bytes) but the two functions are used the same way:

Decode encode

bytes------> str (Unicode)------>bytes

' English ' # Specifies the string type Object ustr = u.encode ('gb2312'# encoded with gb2312 encoding for u, Get bytes type Object stru1 = Str.decode ('gb2312')#  Decodes the string str with GB2312 encoding to get the string type Object u1u2 = Str.decode ('utf-8')#  If you decode str with UTF-8 encoding, you will not be able to restore the original string contents

Python 3 Error resolution using the Binascii method

Related Article

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.