Printing a hex character and prefixing it correctly

X

xamalek

If I have an integer k, for instance;

k = 32 // BASE 10

How do I get print to print it out in HEX and PREFIXED with 0x? What
is the PROPER WAY?

This does not work:

print "This is hex 32: ", int(k, 16)

Xav
 
C

Chris Rebert

If I have an integer k, for instance;

k = 32 // BASE 10

How do I get print to print it out in HEX and PREFIXED with 0x? What
is the PROPER WAY?

This does not work:

 print "This is hex 32: ", int(k, 16)

print "This is hex 32:", hex(k)

print int("0xA", 16) #==> 10

I would recommend you read the tutorial and/or library reference if
you haven't already.

Cheers,
Chris
 
A

Andreas Tawn

If I have an integer k, for instance;
k = 32 // BASE 10

How do I get print to print it out in HEX and PREFIXED with 0x? What
is the PROPER WAY?

This does not work:

print "This is hex 32: ", int(k, 16)

Xav

k = 32

print "This is hex 32: ", hex(k)

Cheers,

Drea
 
B

BgEddy

If I have an integer k, for instance;

k = 32 // BASE 10

How do I get print to print it out in HEX and PREFIXED with 0x? What
is the PROPER WAY?

This does not work:

print "This is hex 32: ", int(k, 16)

Xav

How about this :

k=32
print "This is hex 32: 0x%02X" % k

Kind Regards,
BgEddy
 
D

Dave Angel

If I have an integer k, for instance;

k = 32 // BASE 10

How do I get print to print it out in HEX and PREFIXED with 0x? What
is the PROPER WAY?

This does not work:

print "This is hex 32: ", int(k, 16)

Xav
How about
print "This is hex 32: ", hex(k)


(This works in Python 2.6.2)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,994
Messages
2,570,223
Members
46,813
Latest member
lawrwtwinkle111

Latest Threads

Top