Python module introduction-binascii: Binary and ASCII conversion and other hexadecimal conversion

Source: Internet
Author: User

Python module introduction-binascii: Binary and ASCII conversion and other hexadecimal conversion
2012-08-01 magnet

# Undertake software automation implementation, training, and other gtalk: ouyangchongwu # gmail.com qq 37391319
# Copyright. For reposted content, please contact us by letter

# Automated testing and python group: http://groups.google.com/group/automation_testing_python

# Reference: The Python Standard Library by Example

# Tutorial environment: Python 2.7.3 CentOS release 6.2 (Final) 32 bits

 

 

 

20.1 binascii: Binary and ASCII Conversion
Function: converts binary and ASCII values.

Python version: 1.5 or later

The binascii module contains many binary representation conversion methods in binary and ASCII encoding. Normally, these functions are not directly used, but are encoded in a format such as UU, base64, or BinHex. The binascii module includes lower-level and efficient functions written in C language.

It is used to convert strings and ASCII values. For example:

>>> S = 'hello'

>>> B = b2a_hex (s)

>>> Print B

68656c6c6f

>>> A2b_hex (B)

'Hello'

>>> B = hexlify (s)

>>> Print B

68656c6c6f

>>> Unhexlify (B)

'Hello'

The above functions hexlify and b2a_hex are actually a function. We recommend that you use hexlify. It indicates the hexadecimal representation of the returned binary data. The data of each byte is converted into a two-digit hexadecimal representation. Therefore, the string generated is twice the length of the source data. A2b_hex and unhexlify perform reverse operations.

Note the differences between hexlify and built-in function hex. Hex only converts integers. If you use a string as a parameter, an error is returned.

>>> Hex (23)

'0x17'

>>> Hex (-23)

'-0x17

>>> Hex ('23 ')

Traceback (most recent call last ):

File "<stdin>", line 1, in <module>

TypeError: hex () argument can't be converted to hex

>>>

In addition, float. hex () is the hexadecimal Method for converting floating point numbers, for example:

>>> 1.03.hex ()

'0x1. 07ae147ae147bp + 0'

Int ([x [, base]) converts the string or value to an integer. When the parameter is a string, a base must be set. The default base value is 10, which can be [2, 36] or 0. When the value is 0, the hexadecimal information is obtained from the string. The return value of Int Is a decimal integer. When the input is a floating point number, it is truncated near 0. Similar built-in functions include float and long.

>>> Int (0x17)

23

>>> Int (-0x17)

-23

>>> Int ('23 ')

23

>>> Int ('20140901', 2)

13

>>> Int ('000000', 36)

47953

>>> Int ('20140901', 8)

577

>>>

>>> Int (11.92)

11

>>> Int (-11.92)

-11

Chr (I) returns a single string corresponding to an integer in the ASCII code. For example, chr (97) returns the string 'A '. It is the opposite of ord. This parameter must be in the range of [0 .. 255].

Unichr (I) is the unicode version of chr. For example, unichr (97) returns the string u'a '. The value range of the parameter depends on how Python is configured-this may be UCS2 [0 .. 0 xFFFF] Or UCS4 [0 .. 0x10FFFF].

Ord (c) chr and unichr reverse operations. For example, ord ('A') returns the Integer 97, ord (U' \ u2020') returns 8224. This is the unicode object of CHR () (8-bit string and unichr inverse ). If Python is compiled based on UCS2, the parameter range is [0 .. 65535.

The advantage of Hexlify is that it can process multiple characters at the same time. The following two operations are similar. The returned values are strings, but '0x 'is removed from hexlify '.

>>> Hex (ord ('A '))

'0x61'

>>> Hexlify ('A ')

'61'

Function list:

Binascii. a2b_uu (string)

Binascii. b2a_uu (data)

Binascii. a2b_base64 (string)

Binascii. b2a_base64 (data)

Binascii. a2b_qp (string [, header])

Binascii. b2a_qp (data [, quotetabs, istext, header])

Binascii. a2b_hqx (string)

Binascii. rledecode_hqx (data)

Binascii. rlecode_hqx (data)

Binascii. b2a_hqx (data)

Binascii. crc_hqx (data, crc)

Binascii. crc32 (data [, crc])

Binascii. b2a_hex (data)

Binascii. b2a_hex (data)

Binascii. hexlify (data)

Binascii. a2b_hex (hexstr)

Binascii. unhexlify (hexstr)

Exceptions include:

Exception binascii. Error

Exception binascii. Incomplete

 

Related modules:

Module base64

Support for base64 encoding used inMIME email messages.

Module binhexwww.2cto.com

Support for the binhex format used onthe Macintosh.

Module uu

Support for UU encoding used on Unix.

Module quopri

Support for quoted-printable encodingused in MIME email messages.

 

Author: oychw

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.