1

Today I am reading in a file, and extracting information. I've figured out pretty much everything, but for some reason I am having a very, very annoying problem! I read in an entire line and use the .split() command to break the 'sentence' into 'words' right? And then I alias the 'words' as such:

startAddress = line[ 0 ]
length = line[ 2 ].strip( "(" ).strip( ")" )
...
endAddress = startAddress + length

Note: I strip the length because in the data file it is encased with () which, later, cause problems when I load it into a .csv file because () are used as negatives.

Anyways, if I were to have 0x00230008 be the start address and (4) be the length, my program makes 0x002300084 be the end address instead of 0x00230008C, but if I do hex(length) or hex(startAddress) or even hex(str(length) or hex(str(startAddress)) it throws an error saying hex numbers cannot be converted into hex. Likewise I cannot convert them into integers, either.

Really, all I need to do is add the starting address (which is in Hex, but reads in as a string) and the length (which is in int and reads in as int.) I have tried converting them around, but that didn't work. I also tried the line

endAddress = startAddress + length - 1

which tells me " unsupported operand type(s) for -: 'str' and 'int' " so, I've toyed with it as much as I can, but I'm just not figuring this out. I was thinking of removing the 0x in front of the hex value via strip, but then it reads in as an integer and is incorrect.

The last thing I tried was using line[ 0 ] and line[ 2 ] (with strips) directly to find endAddress, but it gives all the same errors. I tried to force type by stating that startAddress = 0xFFFFFFFF before I assign it equal to line[ 0 ], but that didn't work. So how the heck do I convert a string to a hexidecimal number if it complains that it is hexidecimal when it is not? Or maybe my method of adding them is wrong? Can I use some other adding method?

The biggest confusion for me is that if I try to convert startAddress to a string, and then back into a hexidecimal number, it still complains.

CamelopardalisRex
  • 353
  • 1
  • 2
  • 16
  • I suggest removing the spaces between the parenthesis and square brackets in your code. It is stylistically preferable. – SimonT Jul 29 '13 at 22:27

4 Answers4

2

int takes an optional parameter specifying the base of integer you want to convert it into. So you could simple call something like:

proper_int = int(number, 16)

To get a proper representation.

For example:

int("10", 16) = 16
int("F0", 16) = 240
int("0x10", 16) = 16

If you want to add zero padding I would recommend zfill:

"10".zfill(4) = "0010"
Slater Victoroff
  • 21,376
  • 21
  • 85
  • 144
1

int() defaults to base-10, so specify the base when calling int on a base-16 string:

>>> int('0x00230008', 16)
2293768
Blender
  • 289,723
  • 53
  • 439
  • 496
1

You have to parse the string as a base-16 int

>>> int("0x00230008", 16)
2293768

Add the ints

>>> int("0x00230008", 16) + 4
2293772

And convert it back to a hex string:

>>> hex(int("0x00230008", 16) + 4)
'0x23000c'

You'll have to use some string formatting instead of hex to pad it with zeroes, if you need it:

>>> '0x%08x' % (int("0x00230008", 16) + 4)
'0x0023000c'
Pavel Anossov
  • 60,842
  • 14
  • 151
  • 124
0

Use int or eval function:

>>> int('0x002300084', 16)
36700292
>>> eval('0x002300084')
36700292
>>> hex(36700292)
'0x2300084'

hex, oct and bin functions all take integers and return string
while int takes string (or unicode), and an optional base argument (default to 10) and returns and integer

saeedgnu
  • 4,110
  • 2
  • 31
  • 48
  • 1
    I would recommend against eval. If you want reasons why see this: http://stackoverflow.com/questions/1832940/is-using-eval-in-python-a-bad-practice – Slater Victoroff Jul 29 '13 at 21:44
  • Agreed, but not if you know what you are doing. For example it can make configuration file flexible. You can write numbers in any form (hexadecimal, octal or decimal). And of course if it's a single-user program where you trust the user (which is probably yourself) – saeedgnu Jul 29 '13 at 21:49
  • If you know what you're doing, then there's no reason to use eval in the first place. – Slater Victoroff Jul 29 '13 at 21:53