0

There is a function on mysql:

select inet_ntoa(4294967295 & '-1136577616');

res = 188.65.51.176 

How to make a function on python?

jh314
  • 27,144
  • 16
  • 62
  • 82
avdoshkin
  • 103
  • 4

1 Answers1

1

Python 2.7.3

import socket, struct

packed_value = struct.pack('!I', 4294967295 & int('-1136577616'))
addr = socket.inet_ntoa(packed_value)

print addr

For Python 3.3+ see using inet_ntoa function in Python

Community
  • 1
  • 1
furas
  • 134,197
  • 12
  • 106
  • 148
  • How to get back the number of? In [49]: struct.unpack('>L',socket.inet_aton('188.65.51.176'))[0] Out[49]: 3158389680L – avdoshkin Jul 10 '13 at 03:54
  • 4294967295 & int('-1136577616') == 3158389680L . And you can't get better result. You can't reverse `&` (bit AND). 7 & 1 = 1 and 5 & 1 = 1 so different numbers give the same result. – furas Jul 10 '13 at 04:16
  • How to get the result then? – avdoshkin Jul 10 '13 at 04:24
  • You can't reverse `&` so you can't get `4294967295` or `-1136577616` from `3158389680L`. See also: [How to reverse bitwise AND (&) in C?](http://stackoverflow.com/a/2566235/1832058) – furas Jul 10 '13 at 05:28