title: Compare bytes in Python
body: <p>I get data over TCP and try to compare it with a known value (0xAD):</p>
  <pre><code>b, addr = sock.recvfrom(1)
  h = &quot;&quot;.join(hex(ord(i)) for i in b)
  print h
  if h == str(0xad):
      print &quot;Work&quot;
      data = bytearray()
      data.append(observer.OBSERVER_VALIDATION_BYTE)
      sock.sendto(data, 0, addr)
  </code></pre>
  <p>I tried to compare them like strings as it shown above and tried to compare them like bytes in two ways:</p>
  <pre><code>b[0] == 0xAD
  </code></pre>
  <p>or</p>
  <pre><code>b2 = bytearray()
  b2.append(0xAD)
  b2[0] == b[0]
  </code></pre>
  <p>And all of the comparisons failed, though. <code>print h</code> gives me <code>0xad</code>.</p>
  <p>I have a set of bytes defined like <code>BYTE = 0xAD</code>.
  I need to send them over TCP and compare the read result.</p>
  <p>If I define them like strings (<code>BYTE = '0xAD'</code>), it provides an ability to compare, but I can't put them in the bytearray to send because <code>bytearr.append(BYTE)</code> reasonably returns an error. So I can't redefine them as strings. So what is the way to compare <code>bytes</code> got from <code>sock.recvfrom</code> and value declared in the way I have?</p>