Question

I am trying to convert the following code from c to Python. The C code looks like:

  seed = (time(0) ^ (getpid() << 16));
  fprintf("0x%08x \n", seed);

that outputs values like 0x7d24defb.

And the python code:

  time1 = int(time.time())
  seed  = (time1 ^ (os.getpid() <<16))

that outputs values like: 1492460964

What do i need to modify at the python code so I get address-like values?

Was it helpful?

Solution

It depends on the way the value is displayed. The %x flag in printf-functions displays the given value in hexadecimal. In Python you can use the hex function to convert the value to a hexadecimal representation.

OTHER TIPS

The equivalent Python code to: fprintf("0x%08x \n", seed);

>>> '0x{:08x}"'.format(1492460964)
'0x58f525a4"'

Note that hex() alone won't pad zeros to size 8 like the C code does.

I suppose this is what you what:

>>> n =hex (int(time.time()) ^ (os.getpid() <<16))
>>> print n
0x431c2fd2
>>>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top