954,515 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

How to convince python that a string is actually a hex?

Hello

I have a sequence that looks like this:
60211401dc070000
which is a string (it was build through a series of concatenations and conversions)

How can i just tell him that it is actually a hex ?? without modifying it's value...

Thanks!
Have a nice day

Malraux
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

int(it, 16)

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

I did, and here is the result:
6926839700091502592

update:
so i converted it back to Hex, and it looks good..just that I have A "L" at the end of the sequence
any idee on how to get rid of it

update:
nevermind, thanks anyway

update:
in case someone else has this problem:
it = int(it, 16)
it = hex(it)[2:-1]

Malraux
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

I did, and here is the result: 6926839700091502592

update: so i converted it back to Hex, and it looks good..just that I have A "L" at the end of the sequence any idee on how to get rid of it

update: nevermind, thanks anyway

update: in case someone else has this problem: it = int(it, 16) it = hex(it)[2:-1]

Updated:

It is still treated as a string!!!
HELP!

Malraux
Newbie Poster
4 posts since Jan 2012
Reputation Points: 10
Solved Threads: 0
 

Keep it number to use as number.
if you need to print it out use %x format in print statement.

pyTony
pyMod
Moderator
5,359 posts since Apr 2010
Reputation Points: 782
Solved Threads: 852
 

'int('60211401dc070000', 16)' will be treated as a hexadecimal number, if you want to, say, add another hexadecimal number to it simple prelude it with a '0x'. For example:

>>> a = int('60211401dc070000', 16)
>>> a
6926839700091502592
>>> b = a + 0x000a            # Add the hex number 000a
>>> b
6926839700091502602
>>> hex(b)
'0x60211401dc07000a'
Lucy Four
Newbie Poster
2 posts since Jan 2012
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: