检查一个字符串是否为十六进制 [英] Check if a string is hexadecimal

查看:1604
本文介绍了检查一个字符串是否为十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道最简单的方法是使用正则表达式,但我想知道是否有其他方法可以执行此检查。



为什么我需要这个?我正在编写一个Python脚本,用于从 SIM卡中读取短信(SMS)。在某些情况下,十六进制消息到达,我需要为它们做一些处理,所以我需要检查收到的消息是否是十六进制的。



当我发送以下SMS时:

  Hello world! 

我的脚本收到

pre > 00480065006C006C006F00200077006F0072006C00640021

但在某些情况下,我会收到正常的短信(不是十六进制)。所以我需要做一个如果十六进制控制。



我使用Python 2.6.5。



UPDATE:

这个问题的原因是,(以某种方式)我发送的消息被接收为 hex 运营商发送的消息(信息消息和广告)作为普通字符串接收。所以我决定做一个检查,并确保我有正确的字符串格式的消息。



一些额外的细节:我正在使用华为3G调制解调器和 PyHumod 来读取SIM卡中的数据。





处理这种字符串的最佳方式是使用 a2b_hex (又名 unhexlify )和 utf-16大端编码( as @JonasWielicki提到):

  from binascii import unhexlify#unhexlify是a2b_hex的另一个名称

mystr =00480065006C006C006F00200077006F0072006C00640021
unhexlify(mystr).encode(utf-16-be)
>> u'Hello world!'


解决方案

使用 int()可以很好地实现此目的,而Python的确可以所有为你检查:)

  int('00480065006C006C006F00200077006F0072006C00640021',16)
6896377547970387516320582441726837832153446723333914657L
code>

会起作用。如果发生故障,您将收到 ValueError 异常。 / p>

  int('af',16)
175

int('ah', 16)
...
ValueError:以int 16为基数的无效文字16:'ah'

(2)替代是遍历数据并确保所有字符落在 0的范围内。 .9 af / AF string.hexdigits '0123456789abcdefABCDEF')对此很有用,因为它包含 upper和小写字母。

 输入字符串
全部(c输入字符串中的c.hexdigits)

会返回 True False 基于数据在字符串 s 中的有效性。



简单的例子:

  s ='af'
全部(c在string.hexdigits中用于c in s)
True

s ='ah'
全部(c中的c在string.hexdigits中)
False



备注

正如@ScottGriffiths在a如果你的字符串在开始时包含 0x ,那么 int()方法将起作用,而字符by字符检查将会失败。此外,检查字符设置比字符字符串更快,但是对于SMS短字符串这一点很重要,除非您处理很多(很多!)字符串。在这种情况下,您可以将stringhexditigs转换为一个集合,其中 set(string.hexdigits)


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

00480065006C006C006F00200077006F0072006C00640021

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.

UPDATE:

The reason of that problem is, (somehow) messages I sent are received as hex 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 a2b_hex (a.k.a. unhexlify) and utf-16 big endian encoding (as @JonasWielicki mentioned):

from binascii import unhexlify  # unhexlify is another name of a2b_hex

mystr = "00480065006C006C006F00200077006F0072006C00640021"
unhexlify(mystr).encode("utf-16-be")
>> u'Hello world!'

解决方案

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

int('00480065006C006C006F00200077006F0072006C00640021', 16)
6896377547970387516320582441726837832153446723333914657L

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

Short example:

int('af', 16)
175

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)
True

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

Notes:

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).

这篇关于检查一个字符串是否为十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆