Convert an IP string to a number and vice versa

ip converter
python convert int to ip address
convert ip address to decimal
ip address converter
ip decimal to string online
ip 2 numeric
convert ip address to binary in c
convert ip address from string to integer

How would I use python to convert an IP address that comes as a str to a decimal number and vice versa?

For example, for the IP 186.99.109.000 <type'str'>, I would like to have a decimal or binary form that is easy to store in a database, and then retrieve it.


converting an IP string to long integer:

import socket, struct

def ip2long(ip):
    """
    Convert an IP string to long
    """
    packedIP = socket.inet_aton(ip)
    return struct.unpack("!L", packedIP)[0]

the other way around:

>>> socket.inet_ntoa(struct.pack('!L', 2130706433))
'127.0.0.1'

python - Convert an IP string to a number and vice versa, converting an IP string to long integer: import socket, struct def ip2long(ip): """ Convert an IP string to long """ packedIP = socket.inet_aton(ip) return  #include <arpa/inet.h> /* * Returns a pointer to an internal array containing the string, which is overwritten with each call to the function. * You need to copy the string if you want to keep the value returned. */ extern char * inet_ntoa (struct in_addr in); /* * Convert Internet host address from numbers-and-dots notation in CP * into binary data and store the result in the structure inp


Here's a summary of all options as of 2017-06. All modules are either part of the standard library or can be installed via pip install.

ipaddress module

Module ipaddress (doc) is part of the standard library since v3.3 but it's also available as an external module for python v2.6,v2.7.

>>> import ipaddress
>>> int(ipaddress.ip_address('1.2.3.4'))
16909060
>>> ipaddress.ip_address(16909060).__str__()
'1.2.3.4'
>>> int(ipaddress.ip_address(u'1000:2000:3000:4000:5000:6000:7000:8000'))
21268296984521553528558659310639415296L
>>> ipaddress.ip_address(21268296984521553528558659310639415296L).__str__()
u'1000:2000:3000:4000:5000:6000:7000:8000'
No module import (IPv4 only)

Nothing to import but works only for IPv4 and the code is longer than any other option.

>>> ipstr = '1.2.3.4'
>>> parts = ipstr.split('.')
>>> (int(parts[0]) << 24) + (int(parts[1]) << 16) + \
          (int(parts[2]) << 8) + int(parts[3])
16909060
>>> ipint = 16909060
>>> '.'.join([str(ipint >> (i << 3) & 0xFF)
          for i in range(4)[::-1]])
'1.2.3.4'
Module netaddr

netaddr is an external module but is very stable and available since Python 2.5 (doc)

>>> import netaddr
>>> int(netaddr.IPAddress('1.2.3.4'))
16909060
>>> str(netaddr.IPAddress(16909060))
'1.2.3.4'
>>> int(netaddr.IPAddress(u'1000:2000:3000:4000:5000:6000:7000:8000'))
21268296984521553528558659310639415296L
>>> str(netaddr.IPAddress(21268296984521553528558659310639415296L))
'1000:2000:3000:4000:5000:6000:7000:8000'
Modules socket and struct (ipv4 only)

Both modules are part of the standard library, the code is short, a bit cryptic and IPv4 only.

>>> import socket, struct
>>> ipstr = '1.2.3.4'
>>> struct.unpack("!L", socket.inet_aton(ipstr))[0]
16909060
>>> ipint=16909060
>>> socket.inet_ntoa(struct.pack('!L', ipint))
'1.2.3.4'

Convert an IP string to a number and vice versa, Questions: How would I use python to convert an IP address that comes as a str to a decimal number and vice versa? For example, for the IP  Converting string to number and vice-versa in C++ C++ Server Side Programming Programming In this section we will see how to convert string to number and number to string.


Use class IPAddress in module netaddr.

ipv4 str -> int:

print int(netaddr.IPAddress('192.168.4.54'))
# OUTPUT: 3232236598

ipv4 int -> str:

print str(netaddr.IPAddress(3232236598))
# OUTPUT: 192.168.4.54

ipv6 str -> int:

print int(netaddr.IPAddress('2001:0db8:0000:0000:0000:ff00:0042:8329'))
# OUTPUT: 42540766411282592856904265327123268393

ipv6 int -> str:

print str(netaddr.IPAddress(42540766411282592856904265327123268393))
# OUTPUT: 2001:db8::ff00:42:8329

IPv4 Address to IP Decimal Conversion, This is a free tool to translate IP address from dotted-decimal address to decimal format and vice versa. IP-To-Decimal. IP Address / IP Number. Convert  How to convert a number to a string and vice versa in C ++ Since this question gets asked about every week, this FAQ might help a lot of users. How to convert an integer to a string in C++ how to convert a string into an integer in C++ how to convert a floating-point number to a string in C++ how to convert


Since Python 3.3 there is the ipaddress module that does exactly this job among others: https://docs.python.org/3/library/ipaddress.html. Backports for Python 2.x are also available on PyPI.

Example usage:

import ipaddress

ip_in_int = int(ipaddress.ip_address('192.168.1.1'))
ip_in_hex = hex(ipaddress.ip_address('192.168.1.1'))

Java – Convert IP address to Decimal Number – Mkyong.com, We show you two ways to convert an IP address to a decimal number public long ipToLong(String ipAddress) { String[] ipAddressInArray = ipAddress.split("\\ Stackoverflow : ip address conversion to decimal and vice versa  Converting string to number and vice-versa in C++ In general or more specifically in competitive programming there are many instances where we need to convert a number to a string or string to a number.


Here's One Line Answers:
import socket, struct

def ip2long_1(ip):
    return struct.unpack("!L", socket.inet_aton(ip))[0]

def ip2long_2(ip):
    return long("".join(["{0:08b}".format(int(num)) for num in ip.split('.')]), 2)

def ip2long_3(ip):
    return long("".join(["{0:08b}".format(num) for num in map(int, ip.split('.'))]), 2)

Execution Times:

ip2long_1 => 0.0527065660363234 ( The Best ) ip2long_2 => 0.577211893924598 ip2long_3 => 0.5552745958088666

Two small Python functions to convert IPv4 address to integer and , Two small Python functions to convert IPv4 address to integer and vice-versa - ipv4.py. def from_string(s): join(map(lambda n: str(ip>>n & 0xFF), [24,16,8,0​])). This number value will end up in a String object which doesn't really help your program if you want to do some arithmetic. Fortunately, there are wrapper classes that provide methods for converting those String values into numbers and the String class has a method to convert them back again.


Convert IP to Long and Vice Versa C#, Convert IP to Long and Vice Versa C#. Comments viceversa. 1: static public string LongToIP(long longIP) 6: int num = (int)(longIP / Math. Converting Strings to Numbers and Vice Versa in Java. Java 8 Object Oriented Programming Programming Java lang package provides Integer class which has methods to convert an integer to String and vice versa.


Convert IP address to decimal value (and back), 6 PS > [ Int ]( 18 Converting Binary Data to IP Address (and vice versa). powershelltips. In a previous tip we showed how you  Python Program to Convert Binary Number to Decimal and Vice-Versa A binary number is a number expressed in the base-2 numeral system or binary numeral system, which uses only two symbols 0 and 1. The decimal numeral system is the standard system for denoting integer and non-integer numbers.


Converting Binary Data to IP Address (and vice versa), Here is how you convert a binary text string into the corresponding integer value: $binary = "110110110" $int = [ Convert ] :: ToInt32 ( $binary , 2 ) $int Going the  C++ Program to Convert Binary Number to Decimal and vice-versa In this example, you will learn to convert binary number to decimal, and decimal number to binary manually by creating user-defined functions.