1

I am working in Python with data that contains IPv4 addresses. These IP addresses are under long int format - which I didn't know was a way to store them. I need to have them as string format, such as 10.10.10.10. I have no documentation on this format, I only know it can be converted.

The only decoder I found is an online website that does the correct conversion (https://cafewebmaster.com/online_tools/long2ip).

I'd like to be able to run this kind of code:

In [0]: long2ip(-9223090565996790175)
Out[0]: '10.129.62.97'

Does anyone know how to do the conversion in Python? Many thanks.

gaelchls
  • 23
  • 4

1 Answers1

1

You can convert it with tools in the standard lib

import socket, struct

def convert_long(longip):
    socket.inet_ntoa(struct.pack('!L', longip))
arshbot
  • 12,535
  • 14
  • 48
  • 71
  • This only works for positive long integers, I believe. I forgot to mention that the long ints in my data are negative, such as the example in my post. When I try your proposition, I get 'argument out of range'. – gaelchls Jan 20 '20 at 10:38
  • @gaelchls I don't know of any spec for negative long IPs. The range is usually 0 - 4294967295. If that one online tool works for you, that's a fluke/bug. Perhaps you're misinterpreting some binary representation of a long into a negative number. – deceze Jan 20 '20 at 10:45