/Doc/library/binascii.rst

http://unladen-swallow.googlecode.com/ · ReStructuredText · 178 lines · 113 code · 65 blank · 0 comment · 0 complexity · bbd781974c8f9f1c2bb1f6760e5edb8e MD5 · raw file

  1. :mod:`binascii` --- Convert between binary and ASCII
  2. ====================================================
  3. .. module:: binascii
  4. :synopsis: Tools for converting between binary and various ASCII-encoded binary
  5. representations.
  6. .. index::
  7. module: uu
  8. module: base64
  9. module: binhex
  10. The :mod:`binascii` module contains a number of methods to convert between
  11. binary and various ASCII-encoded binary representations. Normally, you will not
  12. use these functions directly but use wrapper modules like :mod:`uu`,
  13. :mod:`base64`, or :mod:`binhex` instead. The :mod:`binascii` module contains
  14. low-level functions written in C for greater speed that are used by the
  15. higher-level modules.
  16. The :mod:`binascii` module defines the following functions:
  17. .. function:: a2b_uu(string)
  18. Convert a single line of uuencoded data back to binary and return the binary
  19. data. Lines normally contain 45 (binary) bytes, except for the last line. Line
  20. data may be followed by whitespace.
  21. .. function:: b2a_uu(data)
  22. Convert binary data to a line of ASCII characters, the return value is the
  23. converted line, including a newline char. The length of *data* should be at most
  24. 45.
  25. .. function:: a2b_base64(string)
  26. Convert a block of base64 data back to binary and return the binary data. More
  27. than one line may be passed at a time.
  28. .. function:: b2a_base64(data)
  29. Convert binary data to a line of ASCII characters in base64 coding. The return
  30. value is the converted line, including a newline char. The length of *data*
  31. should be at most 57 to adhere to the base64 standard.
  32. .. function:: a2b_qp(string[, header])
  33. Convert a block of quoted-printable data back to binary and return the binary
  34. data. More than one line may be passed at a time. If the optional argument
  35. *header* is present and true, underscores will be decoded as spaces.
  36. .. function:: b2a_qp(data[, quotetabs, istext, header])
  37. Convert binary data to a line(s) of ASCII characters in quoted-printable
  38. encoding. The return value is the converted line(s). If the optional argument
  39. *quotetabs* is present and true, all tabs and spaces will be encoded. If the
  40. optional argument *istext* is present and true, newlines are not encoded but
  41. trailing whitespace will be encoded. If the optional argument *header* is
  42. present and true, spaces will be encoded as underscores per RFC1522. If the
  43. optional argument *header* is present and false, newline characters will be
  44. encoded as well; otherwise linefeed conversion might corrupt the binary data
  45. stream.
  46. .. function:: a2b_hqx(string)
  47. Convert binhex4 formatted ASCII data to binary, without doing RLE-decompression.
  48. The string should contain a complete number of binary bytes, or (in case of the
  49. last portion of the binhex4 data) have the remaining bits zero.
  50. .. function:: rledecode_hqx(data)
  51. Perform RLE-decompression on the data, as per the binhex4 standard. The
  52. algorithm uses ``0x90`` after a byte as a repeat indicator, followed by a count.
  53. A count of ``0`` specifies a byte value of ``0x90``. The routine returns the
  54. decompressed data, unless data input data ends in an orphaned repeat indicator,
  55. in which case the :exc:`Incomplete` exception is raised.
  56. .. function:: rlecode_hqx(data)
  57. Perform binhex4 style RLE-compression on *data* and return the result.
  58. .. function:: b2a_hqx(data)
  59. Perform hexbin4 binary-to-ASCII translation and return the resulting string. The
  60. argument should already be RLE-coded, and have a length divisible by 3 (except
  61. possibly the last fragment).
  62. .. function:: crc_hqx(data, crc)
  63. Compute the binhex4 crc value of *data*, starting with an initial *crc* and
  64. returning the result.
  65. .. function:: crc32(data[, crc])
  66. Compute CRC-32, the 32-bit checksum of data, starting with an initial crc. This
  67. is consistent with the ZIP file checksum. Since the algorithm is designed for
  68. use as a checksum algorithm, it is not suitable for use as a general hash
  69. algorithm. Use as follows::
  70. print binascii.crc32("hello world")
  71. # Or, in two pieces:
  72. crc = binascii.crc32("hello")
  73. crc = binascii.crc32(" world", crc) & 0xffffffff
  74. print 'crc32 = 0x%08x' % crc
  75. .. note::
  76. To generate the same numeric value across all Python versions and
  77. platforms use crc32(data) & 0xffffffff. If you are only using
  78. the checksum in packed binary format this is not necessary as the
  79. return value is the correct 32bit binary representation
  80. regardless of sign.
  81. .. versionchanged:: 2.6
  82. The return value is in the range [-2**31, 2**31-1]
  83. regardless of platform. In the past the value would be signed on
  84. some platforms and unsigned on others. Use & 0xffffffff on the
  85. value if you want it to match 3.0 behavior.
  86. .. versionchanged:: 3.0
  87. The return value is unsigned and in the range [0, 2**32-1]
  88. regardless of platform.
  89. .. function:: b2a_hex(data)
  90. hexlify(data)
  91. Return the hexadecimal representation of the binary *data*. Every byte of
  92. *data* is converted into the corresponding 2-digit hex representation. The
  93. resulting string is therefore twice as long as the length of *data*.
  94. .. function:: a2b_hex(hexstr)
  95. unhexlify(hexstr)
  96. Return the binary data represented by the hexadecimal string *hexstr*. This
  97. function is the inverse of :func:`b2a_hex`. *hexstr* must contain an even number
  98. of hexadecimal digits (which can be upper or lower case), otherwise a
  99. :exc:`TypeError` is raised.
  100. .. exception:: Error
  101. Exception raised on errors. These are usually programming errors.
  102. .. exception:: Incomplete
  103. Exception raised on incomplete data. These are usually not programming errors,
  104. but may be handled by reading a little more data and trying again.
  105. .. seealso::
  106. Module :mod:`base64`
  107. Support for base64 encoding used in MIME email messages.
  108. Module :mod:`binhex`
  109. Support for the binhex format used on the Macintosh.
  110. Module :mod:`uu`
  111. Support for UU encoding used on Unix.
  112. Module :mod:`quopri`
  113. Support for quoted-printable encoding used in MIME email messages.