1

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?

biggdman
  • 2,028
  • 7
  • 32
  • 37

3 Answers3

6

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.

Kninnug
  • 7,992
  • 1
  • 30
  • 42
1

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.

jamylak
  • 128,818
  • 30
  • 231
  • 230
1

I suppose this is what you what:

>>> n =hex (int(time.time()) ^ (os.getpid() <<16))
>>> print n
0x431c2fd2
>>>
jamylak
  • 128,818
  • 30
  • 231
  • 230
kelvin
  • 149
  • 7