Printing negative values as hex in python

前端 未结 1 692
时光说笑
时光说笑 2021-01-17 21:56

I have the following code snippet in C++:

for (int x = -4; x < 5; ++x)
       printf(\"hex x %d 0x%08X\\n\", x, x);

And its output is

1条回答
  •  离开以前
    2021-01-17 22:43

    You need to explicitly restrict the integer to 32-bits:

    for x in range(-4,5):
       print "hex x %d 0x%08X" % (x, x & 0xffffffff)
    

    0 讨论(0)
提交回复
热议问题