Reputation:
I have been given a hex value that is being treated as an int by python. I need to get this value treated as hex without changing the value. I tried using data = hex(int(data, 16)) but this makes my data a string. Is there a way to change an int to a hex without actually changing the representation?
Ex: data = 9663676416, type(data) = int I want: data = 0x9663676416, type(data) = hex
Upvotes: 1
Views: 1775
Reputation: 114038
x = int(str(9663676416),16)
print x
print hex(x)
as mentioned in the comments what you have is a number .... all you do is mess with the representation ...
if you want type(x) == hex
you will need to do something else (tbh im not sure what ... you can certainly do stuff like isinstance(x,hexNumber)
class hexNumber:
def __init__(self,x):
if isinstance(x,str):
self.val = int(x,16)
elif isinstance(x,int):
self.val = int(str(x),16)
def __str__(self):
return hex(self.val)
y = hexNumber(12345)
print "%s"%y
print isinstance(y,hexNumber)
y = hexNumber("1234abc")
print "%s"%y
print isinstance(y,hexNumber)
Upvotes: 3