Converting a 32-bit integer into IP

January 2017

A program in Perl language can be used to convert a 32 bit integer to an IP address. Some knowledge of programming concepts and scripting languages like Perl is required, to write a program code in Perl. Basic concepts and knowledge of binary operations and regular expressions are necessary to write programs. A program to convert a 32 bit integer to an IP address in Perl can be written by using basic binary operations. With binary operations and Perl, the program code for the conversion of a 32 bit integer to a valid IP address can be accomplished in a few lines.



In this example the number to be converted is : 3265917058

Binary representation


11000010 10101001 11110000 10000010 - 3265917058     
00000000 00000000 00000000 11000010 - 3265917058 >> 24 ( 194 )     

11000010 10101001 11110000 10000010 - 3265917058     
10101001 11110000 10000010 00000000 - 3265917058 << 8     
00000000 00000000 00000000 10101001 - (3265917058 << 8 ) >> 24 ( 169 )     

11000010 10101001 11110000 10000010 - 3265917058     
11110000 10000010 00000000 00000000 - 3265917058 << 16     
00000000 00000000 00000000 11110000 - (3265917058 << 16) >> 24 ( 240 )     

11000010 10101001 11110000 10000010 - 3265917058     
10000010 00000000 00000000 00000000 - 3265917058 << 24     
00000000 00000000 00000000 10000010 - (3265917058 << 24) >> 24 ( 130 )

Command use for conversion


$ echo 3265917058 | perl -ne 'print $_>>24 ,".",$_<<8>>24,".",$_<<16>>24,".",$_<<24>>24,"\n"'     
194.169.240.130

Related


Published by netty5. Latest update on November 23, 2011 at 05:07 AM by Rik Sharma.
This document, titled "Converting a 32-bit integer into IP," is available under the Creative Commons license. Any copy, reuse, or modification of the content should be sufficiently credited to CCM (ccm.net).