FallenAngel FallenAngel - 1 year ago 494
Python Question

Check if a string is hexadecimal

I know the easiest way is using a regular expression, but I wonder if there are other ways to do this check.

Why do I need this? I am writing a Python script that reads text messages (SMS) from a SIM 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.

When I send following SMS:

Hello world!

And my script receives


But in some situations, I receive normal text messages (not hex). So I need to do a if hex control.

I am using Python 2.6.5.


The reason of that problem is, (somehow) messages I sent are received as
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.

Some extra details: I am using a Huawei 3G modem and PyHumod to read data from the SIM card.

Possible best solution to my situation:

The best way to handle such strings is using
) and
utf-16 big endian encoding
(as @JonasWielicki mentioned):

from binascii import unhexlify # unhexlify is another name of a2b_hex

mystr = "00480065006C006C006F00200077006F0072006C00640021"
>> u'Hello world!'

Answer Source

(1) Using int() works nicely for this, and Python does all the checking for you :)

int('00480065006C006C006F00200077006F0072006C00640021', 16)

will work. In case of failure you will receive a ValueError exception.

Short example:

int('af', 16)

int('ah', 16)
ValueError: invalid literal for int() with base 16: 'ah'

(2) An alternative would be to traverse the data and make sure all characters fall within the range of 0..9 and a-f/A-F. string.hexdigits ('0123456789abcdefABCDEF') is useful for this as it contains both upper and lower case digits.

import string
all(c in string.hexdigits for c in s)

will return either True or False based on the validity of your data in string s.

Short example:

s = 'af'
all(c in string.hexdigits for c in s)

s = 'ah'
all(c in string.hexdigits for c in s)


As @ScottGriffiths notes correctly in a comment below, the int() approach will work if your string contains 0x at the start, while the character-by-character check will fail with this. Also, checking against a set of characters is faster than a string of characters, but it is doubtful this will matter with short SMS strings, unless you process many (many!) of them in sequence in which case you could convert stringhexditigs to a set with set(string.hexdigits).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download