Sending hex-data via socket, is interpreted as string
Question: I'm new to python, and I'm trying to use a 3rd party library/module. What I'm
doing now is:

    
    
    s.send(rtp.header_bytes + rtp.payload) 
    # -> \x80!\x00\x01\x00\x00\x00d\x00\x00\x00\x00Testy
    

to send the header and the payload of a packet via tcp socket. On the
receiving side:

    
    
    conn, addr = socket_rtp.accept()
    data = conn.recv(1024)
    

the data is passed to a constructor `__init__(self, bytes, length)` and then
used like this

    
    
    self.version = (bytes[0] & Packet.V_MASK) >> 6
    

but it seems that it's interpreting the bytes array as string? I'm getting the
following exception:

    
    
    .... line 322, in __init__
        self.version = (bytes[0] & Packet.V_MASK) >> 6
    TypeError: unsupported operand type(s) for &: 'str' and 'int'
    

Thank you for your help.


Answer: To convert the string (`bytes`) to int, you can use
[`ord`](https://docs.python.org/2/library/functions.html#ord) (assuming
your're using Python 2.x):

    
    
    self.version = (bytes[0] & Packet.V_MASK) >> 6
    

or
[`struct.unpack`](https://docs.python.org/2/library/struct.html#struct.unpack):

    
    
    self.version = (struct.unpack('B', bytes[0])[0] & Packet.V_MASK) >> 6
    

* * *
    
    
    >>> ord('\x80')
    128
    >>> import struct
    >>> struct.unpack('B', '\x80')[0]
    128
    

