Movatterモバイル変換


[0]ホーム

URL:


Up one LevelPython Library ReferenceContentsModule IndexIndex

7.13binascii -- Convert between binary and ASCII

Thebinascii module contains a number of methods to convertbetween binary and various ASCII-encoded binaryrepresentations. Normally, you will not use these functions directlybut use wrapper modules likeuu,base64, orbinhex instead. Thebinascii modulecontains low-level functions written in C for greater speedthat are used by the higher-level modules.

Thebinascii module defines the following functions:

a2b_uu(string)
Convert a single line of uuencoded data back to binary and return thebinary data. Lines normally contain 45 (binary) bytes, except for thelast line. Line data may be followed by whitespace.

b2a_uu(data)
Convert binary data to a line of ASCII characters, the return valueis the converted line, including a newline char. The length ofdata should be at most 45.

a2b_base64(string)
Convert a block of base64 data back to binary and return thebinary data. More than one line may be passed at a time.

b2a_base64(data)
Convert binary data to a line of ASCII characters in base64 coding.The return value is the converted line, including a newline char.The length ofdata should be at most 57 to adhere to the base64standard.

a2b_qp(string[, header])
Convert a block of quoted-printable data back to binary and return thebinary data. More than one line may be passed at a time.If the optional argumentheader is present and true, underscoreswill be decoded as spaces.

b2a_qp(data[, quotetabs, istext, header])
Convert binary data to a line(s) of ASCII characters inquoted-printable encoding. The return value is the converted line(s).If the optional argumentquotetabs is present and true, all tabsand spaces will be encoded. If the optional argumentistext is present and true,newlines are not encoded but trailing whitespace will be encoded.If the optional argumentheader ispresent and true, spaces will be encoded as underscores per RFC1522.If the optional argumentheader is present and false, newlinecharacters will be encoded as well; otherwise linefeed conversion mightcorrupt the binary data stream.

a2b_hqx(string)
Convert binhex4 formatted ASCII data to binary, without doingRLE-decompression. The string should contain a complete number ofbinary bytes, or (in case of the last portion of the binhex4 data)have the remaining bits zero.

rledecode_hqx(data)
Perform RLE-decompression on the data, as per the binhex4standard. The algorithm uses0x90 after a byte as a repeatindicator, followed by a count. A count of0 specifies a bytevalue of0x90. The routine returns the decompressed data,unless data input data ends in an orphaned repeat indicator, in whichcase theIncomplete exception is raised.

rlecode_hqx(data)
Perform binhex4 style RLE-compression ondata and return theresult.

b2a_hqx(data)
Perform hexbin4 binary-to-ASCII translation and return theresulting string. The argument should already be RLE-coded, and have alength divisible by 3 (except possibly the last fragment).

crc_hqx(data, crc)
Compute the binhex4 crc value ofdata, starting with an initialcrc and returning the result.

crc32(data[, crc])
Compute CRC-32, the 32-bit checksum of data, starting with an initialcrc. This is consistent with the ZIP file checksum. Since thealgorithm is designed for use as a checksum algorithm, it is notsuitable for use as a general hash algorithm. Use as follows:
    print binascii.crc32("hello world")    # Or, in two pieces:    crc = binascii.crc32("hello")    crc = binascii.crc32(" world", crc)    print crc

b2a_hex(data)
hexlify(data)
Return the hexadecimal representation of the binarydata. Everybyte ofdata is converted into the corresponding 2-digit hexrepresentation. The resulting string is therefore twice as long asthe length ofdata.

a2b_hex(hexstr)
unhexlify(hexstr)
Return the binary data represented by the hexadecimal stringhexstr. This function is the inverse ofb2a_hex().hexstr must contain an even number of hexadecimal digits (whichcan be upper or lower case), otherwise aTypeError israised.

exception Error
Exception raised on errors. These are usually programming errors.

exception Incomplete
Exception raised on incomplete data. These are usually not programmingerrors, but may be handled by reading a little more data and tryingagain.

See Also:

Modulebase64:
Support for base64 encoding used in MIME email messages.

Modulebinhex:
Support for the binhex format used on the Macintosh.

Moduleuu:
Support for UU encoding used onUnix.

Modulequopri:
Support for quoted-printable encoding used in MIME email messages. .


Up one LevelPython Library ReferenceContentsModule IndexIndex

Release 2.5.2, documentation updated on 21st February, 2008.
SeeAbout this document... for information on suggesting changes.
[8]ページ先頭

©2009-2025 Movatter.jp