Differences between revisions 1 and 10 (spanning 9 versions)
Revision 1 as of 2004-11-11 15:42:52
Size: 613
Editor: 157
Comment:
Revision 10 as of 2011-03-26 23:10:50
Size: 1338
Editor: PaulBoddie
Comment: Added categories.
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
## page was renamed from How can I convert a hex representation to an integer?
Line 16: Line 17:

['anon']: try
{{{
#!python
>>> 0x00cc15a4
13374884

}}}



[[lwickjr]]: you use

{{{
#!python
>>> print int('0a', 16)
10
>>>
}}}

...but these aren`t hex values. Try ord() on each character, and see if that gives you the results you need.


YerMat:

See module [[http://docs.python.org/lib/module-struct.html|struct]] !

{{{
#!python
>>> import struct
>>> struct.unpack('I', '\x00\xcc\x15\xa4')
(2752891904L,)
>>> struct.unpack('i', '\x00\xcc\x15\xa4')
(-1542075392,)
>>>
}}}

== See Also ==

 * BitManipulation has a bunch on working with hex
----
CategoryAskingForHelp CategoryAskingForHelpAnswered

Converting a Hex representation to an Integer

I'm running into some trouble when trying to convert a series of hex representations into an integer... I'm sure there is a builtin to do this (wouldn't make sense not to have one) but I can't find it for the life of me.

This is a dump of my interpreter session:

   1 >>> file=open("C:/test.m4a")
   2 >>> contents=file.read()
   3 >>> contents.find('user')
   4 592
   5 >>> contents[592:600]
   6 'user\x00\xcc\x15\xa4'

and I need to convert the values following user ('\x00\xcc\x15\xa4') into an integer value... what am I doing wrong here?

['anon']: try

   1 >>> 0x00cc15a4
   2 13374884

lwickjr: you use

   1 >>> print int('0a', 16)
   2 10
   3 >>>

...but these aren`t hex values. Try ord() on each character, and see if that gives you the results you need.

YerMat:

See module struct !

   1 >>> import struct
   2 >>> struct.unpack('I', '\x00\xcc\x15\xa4')
   3 (2752891904L,)
   4 >>> struct.unpack('i', '\x00\xcc\x15\xa4')
   5 (-1542075392,)
   6 >>>

See Also


CategoryAskingForHelp CategoryAskingForHelpAnswered

Asking for Help/How can I convert a hex representation to an integer? (last edited 2011-03-26 23:10:50 by PaulBoddie)

Unable to edit the page? See the FrontPage for instructions.