title: Check if a string is hexadecimal
body: <p>I know the easiest way is using a <a href="http://en.wikipedia.org/wiki/Regular_expression">regular expression</a>, but I wonder if there are other ways to do this check.</p>
  
  <p>Why do I need this? I am writing a Python script that reads text messages (SMS) from a <a href="https://en.wikipedia.org/wiki/Subscriber_identity_module">SIM</a> card. In some situations, hex messages arrives and I need to do some processing for them, so I need to check if a received message is hexadecimal.</p>
  
  <p>When I send following SMS:</p>
  
  <pre><code>Hello world!
  </code></pre>
  
  <p>And my script receives</p>
  
  <pre><code>00480065006C006C006F00200077006F0072006C00640021
  </code></pre>
  
  <p>But in some situations, I receive normal text messages (not hex). So I need to do a <strong>if hex</strong> control.</p>
  
  <p>I am using Python 2.6.5.</p>
  
  <p>UPDATE:</p>
  
  <p>The reason of that problem is, (somehow) messages I sent are received as <code>hex</code> while messages sent by operator (info messages and ads.) are received as a normal string. So I decided to make a check and ensure that I have the message in the correct string format.</p>
  
  <p><strong>Some extra details</strong>: I am using a Huawei 3G modem and <a href="http://code.google.com/p/pyhumod/">PyHumod</a> to read data from the SIM card.</p>
  
  <p><strong><em>Possible best solution to my situation:</em></strong></p>
  
  <p>The best way to handle such strings is using <code>a2b_hex</code> (a.k.a. <code>unhexlify</code>) and <code>utf-16 big endian encoding</code> (as @JonasWielicki mentioned):</p>
  
  <pre><code>from binascii import unhexlify  # unhexlify is another name of a2b_hex
  
  mystr = "00480065006C006C006F00200077006F0072006C00640021"
  unhexlify(mystr).encode("utf-16-be")
  &gt;&gt; u'Hello world!'
  </code></pre>