#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    Converting hex to dec


    Hi. I've got something similar to the following:

    24523 00:44:33:22 3F5 CBST
    --
    --
    --
    --
    51462 33:44:55:13 200 CBST

    The third column is filled with hex numbers. i need to convert it into decimal numbers...amoung other things.

    i'm reading in the file containing this data, and reading it line by line.
    this is a one time use script so i have cut some corners to pull out the third column, as in Line[21:23] --> the hex number



    i'm coding:

    Hex = Line[21:23]
    print(int(Hex,16)

    i get this error:

    File "U:\PRIVATE\Python\CBST2.py", line 87, in doit
    print(int(Hex),16)
    ValueError: invalid literal for int() with base 10: ''

    Any suggestions greatly appreciated.
  2. #2
  3. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    The "3" of "3F5" is at index 18.

    24523 00:44:33:22 3F5 CBST

    If you know the third field is the hex field, use
    hexidecimal_string = line.split()[2]

    Comments on this post

    • WynnDeezl agrees
    [code]Code tags[/code] are essential for python code and Makefiles!
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2011
    Posts
    139
    Rep Power
    3

    thanks


    there are actually two spaces between each column.

    wow. i didn't know i could use split without a split character, i.e. line.split('char of some sort'). i tried split with a space as the character and since there are two in between it didn't work. i'll use the line.split() with nothing in the ().

    thanks!!!
  6. #4
  7. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,711
    Rep Power
    480
    The default split character is white space, or something like that. You could look it up in the manual.
    [code]Code tags[/code] are essential for python code and Makefiles!

IMN logo majestic logo threadwatch logo seochat tools logo