Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
491 views
in Technique[技术] by (71.8m points)

Declaring a variable as hex in Python

I have a variable

s=64

This variable is in hex. But Python takes it as decimal. How do I declare it as a hex variable?

I know to declare something as hex, we use

s=0x64

But I have only

s=64

How can I go about this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

The number 64 in hex (base 16) is 100. To achieve you answer, you should use a combination of hex(), int() and str() If you start with s = 64 and you want to end up with s = 100 which is the decimal value of 0x64, consider this code:

s = 64
s = int(str(s), 16)

If you want something else, please clarify what, and note that you can try to achieve it yourself with some combination of hex(), int() and str()


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...