title: how to get the integer value of a single pyserial byte in python
body: <p>I'm using pyserial in python 2.7.5 which according to the <a href="http://pyserial.sourceforge.net/pyserial_api.html" rel="noreferrer">docs</a>:</p>
  
  <blockquote>
    <p>read(size=1)
    Parameters: 
    size – Number of bytes to read.
    Returns:<br>
    Bytes read from the port.
    Read size bytes from the serial port. If a timeout is set it may return less characters as requested. With no timeout it will block until the requested number of bytes is read.</p>
    
    <p>Changed in version 2.5: Returns an instance of bytes when available (Python 2.6 and newer) and str otherwise.</p>
  </blockquote>
  
  <p>Mostly I want to use the values as hex values and so when I use them I use the following code:</p>
  
  <pre><code>ch = ser.read()
  print ch.encode('hex')
  </code></pre>
  
  <p>This works no problem. </p>
  
  <p>But now I'm trying to read just ONE value as an integer, because it's read in as a string from serial.read, I'm encountering error after error as I try to get an integer value. </p>
  
  <p>For example: </p>
  
  <pre><code>print ch
  </code></pre>
  
  <p>prints nothing because it's an invisible character (in this case chr(0x02)). </p>
  
  <pre><code>print int(ch)
  </code></pre>
  
  <p>raises an error</p>
  
  <pre><code>ValueError: invalid literal for int() with base 10: '\x02'
  </code></pre>
  
  <p>trying <code>print int(ch,16)</code>, <code>ch.decode()</code>, <code>ch.encode('dec')</code>, <code>ord(ch)</code>, <code>unichr(ch)</code> all give errors (or nothing). </p>
  
  <p>In fact, the only way I have got it to work is converting it to hex, and then back to an integer:</p>
  
  <pre><code>print int(ch.encode('hex'),16)
  </code></pre>
  
  <p>this returns the expected <code>2</code>, but I know I am doing it the wrong way. How do I convert a a chr(0x02) value to a 2 more simply?</p>
  
  <p>Believe me, I have searched and am finding ways to do this in python 3, and work-arounds using imported modules. Is there a native way to do this without resorting to importing something to get one value? </p>
  
  <p>edit: I have tried <code>ord(ch)</code> but it is returning <code>90</code> and I KNOW the value is <code>2</code>, 1) because that's what I'm expecting, and 2) because when I get an error, it tells me (as above)</p>
  
  <p>Here is the code I am using that generates <code>90</code></p>
  
  <pre><code>count = ser.read(1)
  print "count:",ord(ch)
  </code></pre>
  
  <p>the output is <code>count: 90</code></p>
  
  <p>and as soon as I cut and pasted that code above I saw the error <code>count != ch</code>!!!!</p>
  
  <p>Thanks</p>