Search code examples
pythonbytearrays

Convert byte string to bytes or bytearray


I have a string as follows:

  b'\x00\x00\x00\x00\x07\x80\x00\x03'

How can I convert this to an array of bytes? ... and back to a string from the bytes?


Solution

  • in python 3:

    >>> a=b'\x00\x00\x00\x00\x07\x80\x00\x03'
    >>> b = list(a)
    >>> b
    [0, 0, 0, 0, 7, 128, 0, 3]
    >>> c = bytes(b)
    >>> c
    b'\x00\x00\x00\x00\x07\x80\x00\x03'
    >>>