smithy
smithy

Reputation: 601

How do I change a decimal string to a hex formatted string?

How do I change a decimal string like 123456789 to a hex formatted string like this:

07:5B:CD:15

I have looked at the hex function, but it doesn't give me the above format.

UPDATED

I would also like -123456789 to be:

-07:5B:CD:15 

rather than

-7:5B:CD:15

Upvotes: 0

Views: 138

Answers (1)

eumiro
eumiro

Reputation: 212885

s = '123456789'
a = '{:08X}'.format(int(s))
print ':'.join(a[i:i+2] for i in xrange(0, len(a), 2))

prints

07:5B:CD:15

Upvotes: 5

Related Questions