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?

0

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

1

I suppose this is what you what:

>>> n =hex (int(time.time()) ^ (os.getpid() <<16))
>>> print n
0x431c2fd2
>>>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.