convert ascii to integer like '\x01' inputs
Question: I get data from network in bytearray and I need to get integer value in
bytearray[i]. It is ASCII and when I try to convert integer with int() I get
exception. How can I try to convert '\x01' to get 1 in python? Thanks.


Answer: Use [`ord`](http://docs.python.org/2/library/functions.html#ord):

    
    
    >>> ord('\x01')
    1
    

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

    
    
    >>> import struct
    >>> struct.unpack('B', '\x01')
    (1,)
    >>> struct.unpack('2B', '\x01\x02')
    (1, 2)
    

