binascii
Starlark module for working with binascii libarary.
The binascii module contains a number of methods to convert between binary and various ASCII-encoded binary representations. Normally, you will not use these functions directly but use wrapper modules like uu, base64, or binhex instead. The binascii module contains low-level functions written in C for greater speed that are used by the higher-level modules.
Similar to binascii in Python.
binascii.a2b_base64(data)
Convert a block of base64 data back to binary and return the binary data. More than one line may be passed at a time.
Example:
binascii.a2b_base64('bGFya3k=')
b'larky'
Parameters:
data – base64 encoded string.
Returns: decoded binary data.
binascii.a2b_hex(hexstr)
Return the binary data represented by the hexadecimal string hexstr. This function is the inverse of b2a_hex(). hexstr must contain an even number of hexadecimal digits (which can be upper or lower case), otherwise an Error exception is raised.
Similar functionality (accepting only text string arguments, but more liberal towards whitespace) is also accessible using the bytes.fromhex() method.
Same as unhexlify()
binascii.a2b_hex('6c61726b79')
b'larky'
Parameters:
hexstr – hexadecimal encoded string (must contain an even number of hex digits (upper or lower case)).
Returns: decoded binary data.
binascii.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 if newline is true. The output of this function conforms to RFC 3548.
binascii.b2a_hex('larky')
b'bGFya3k='
Parameters:
data – bytes string.
Returns: base64 encoded bytes.
binascii.b2a_hex(data)
Return the hexadecimal representation of the binary data. Every byte of data is converted into the corresponding 2-digit hex representation. The returned bytes object is therefore twice as long as the length of data.
Similar functionality (but returning a text string) is also conveniently accessible using the bytes.hex() method.
If sep is specified, it must be a single character str or bytes object. It will be inserted in the output after every bytes_per_sep input bytes. Separator placement is counted from the right end of the output by default, if you wish to count from the left, supply a negative bytes_per_sep value. Same as hexlify()
Example:
binascii.b2a_hex(b'\xb9\x01\xef')
b'b901ef'
binascii.hexlify(b'\xb9\x01\xef', ':')
b'b9:01:ef'
binascii.b2a_hex(b'\xb9\x01\xef', b'_', 2)
b'b9_01ef'
Parameters:
data – bytes string.
Returns: binary data in hexadecimal.
binascii.crc32(data, value=0)
Compute CRC-32, the 32-bit checksum of data, starting with an initial CRC of value. The default initial CRC is zero. The algorithm is consistent with the ZIP file checksum. Since the algorithm is designed for use as a checksum algorithm, it is not suitable for use as a general hash algorithm.
binascii.crc32(b'larky')
3789503229
crc = binascii.crc32(b'larky')
binascii.crc32(b'larky', crc)
257754740
Parameters:
data – bytes string.
value – initial CEC.
Returns: data crc32 checksum.
binascii.hexlify(data)
Hexadecimal representation of binary data The return value is a bytes object. Same as b2a_hex()
Parameters:
data – bytes string.
Returns: binary data in hexadecimal.
binascii.unhexlify(hexstr)
Binary data of hexadecimal representation Same as a2b_hex()
Parameters:
hexstr – hexadecimal encoded string (must contain an even number of hex digits (upper or lower case)).
Returns: decoded binary data.
Last updated