When I run:
dec = 255
print hex(dec)
I get 0xff, but I like to get just 'ff'
This should do what you want:
print "%x" % 255
When I run:
dec = 255 print hex(dec)
I get 0xff, but I like to get just 'ff'
'0x' always appears in front, so if you want to use hex() and get rid of 0x, use hex(255)[2:]
a=input("enter number in decimal :")
def cal(a):
if a<10:
b=str(a)
elif a==10:
b="A"
elif a==11:
b="B"
elif a==12:
b="C"
elif a==13:
b="D"
elif a==14:
b="E"
else:
b="F"
return b
b=a
L1=""
while True:
c=b%16
if b<16:
L1=L1+cal(c)
break
else:
L1=L1+cal(c)
b=b/16
c=""
for i in reversed(L1):
c=c+i
print c
this might be a bit cleaner than warunn's code
h=[str(i) for i in range(10)]+["A","B","C","D","E","F"]
def hexa(dec):
s=""
while dec:
s+=h[dec%16]
dec//=16
return s[::-1]
What about this:
d = 30
hex(d).split('x')[1]
'1e'
hex(d)[2:]
The other question is, does your hex string convert back to decimal?
a = 255
hx = hex(a)
print(hx)
print(int(hx, 16))
hx = hex(a)[2:]
print(hx)
print(int(hx, 16))
''' output -->
0xff
255
ff
255
'''
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.