Skip to content

Instantly share code, notes, and snippets.

@LuoZijun
Last active August 18, 2022 10:51
Show Gist options
  • Save LuoZijun/df2d57ab6f5217a4bd18 to your computer and use it in GitHub Desktop.
Save LuoZijun/df2d57ab6f5217a4bd18 to your computer and use it in GitHub Desktop.
IPv4 Address to a 32-bit integer value
#!/usr/bin/env python
#-*- coding:utf-8 -*-
"""
IPv4 addresses to a 32-bit integer value
Document: https://en.wikipedia.org/wiki/IPv4#Address_representations
IPv4 addresses may be in any notation expressing a 32-bit integer value,
but for human convenience, they are most often written in the dot-decimal notation,
which consists of four octets of the address expressed individually in decimal and separated by periods.
An IP address followed by a slash(/) and a number (i.e. 127.0.0.1/8 ) indicates a block of addresses using a subnet mask.
See CIDR notation.
The following table shows several representation formats:
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Notation | Value | Conversion from dot-decimal |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Dotted decimal | 192.0.2.235 | N/A |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Dotted hexadecimal | 0xC0.0x00.0x02.0xEB | Each octet, preceded by 0x, is individually converted to hexadecimal form. |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Dotted octal | 0300.0000.0002.0353 | Each octet, preceded by 0, is individually converted into octal. |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Hexadecimal | 0xC00002EB | The 32-bit number is expressed as the concatenation of the octets from the dotted hexadecimal. |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Decimal | 3221226219 | The 32-bit number is expressed in decimal. |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
| Octal | 030000001353 | The 32-bit number is expressed in octal. |
+--------------------+-----------------------+-------------------------------------------------------------------------------------------------+
Mixing decimal, octal and hexadecimal is allowed in dotted format per octet.
Note that in non-dotted formats, numbers bigger than 32-bit, can be given in some cases (e.g. Firefox) and will get converted mod 232.
"""
def i2Hex (n):
hstr = str(hex(int(n)))
if "0x" in hstr:
if len(hstr[2:]) == 1: return "0" + hstr[2:].upper()
else: return hstr[2:].upper()
else:
return hstr.upper()
def hexadecimal (ip_addr):
# 0xC00002EB ( Hexadecimal )
return "0x" + "".join( map( i2Hex, ip_addr.split(".") ) )
def hexadecimal_with_dotted (ip_addr):
# 0xC0.0x00.0x02.0xEB ( Dotted hexadecimal )
hex_s = []
for hs in map( i2Hex, ip_addr.split(".") ):
hex_s.append("0x" + hs)
return ".".join(hex_s)
def test():
# IPv4 address ( Dotted decimal )
ip_address = "192.0.2.235"
print ": @@ Test : %s\n" % ip_address
print hexadecimal( ip_address )
print hexadecimal_with_dotted( ip_address )
if __name__ == "__main__":
test()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment