Re: hex check

Discussion in 'Python' started by Troels Therkelsen, Jul 16, 2003.

  1. In article <>, Ruslan Spivak wrote:
    > Hello.
    >
    > Does anybody have a hint how to check if input hex number is in correct
    > hex format?
    >
    > Thanks in advance.


    You could try the int() built-in function:

    Python 2.3b2 (#1, Jun 30 2003, 13:04:39)
    [GCC 2.95.3 20010315 (release)] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> int("f3", 16)

    243
    >>> int("0xf3", 16)

    243
    >>> int("f3g", 16)

    Traceback (most recent call last):
    File "<stdin>", line 1, in ?
    ValueError: invalid literal for int(): f3g
    >>>


    You'd have to wrap it in a construct like this, for example:

    inputhex = "f3"
    try:
    i = int(inputhex, 16)
    except ValueErr:
    print "Bad hex input: %s" % inputhex


    Hope this hint helps,

    Troels Therkelsen.
    Troels Therkelsen, Jul 16, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    10
    Views:
    6,181
    Neredbojias
    Aug 19, 2005
  2. Bengt Richter
    Replies:
    6
    Views:
    461
    Juha Autero
    Aug 19, 2003
  3. jack
    Replies:
    4
    Views:
    583
  4. tim

    hex string to hex value

    tim, Nov 22, 2005, in forum: Python
    Replies:
    8
    Views:
    18,837
  5. tim
    Replies:
    2
    Views:
    1,553
    Dennis Lee Bieber
    Nov 23, 2005
Loading...

Share This Page