0
Thanks

A few words of thanks would be greatly appreciated.

Converting a 32-bit integer into IP

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
0
Thanks

A few words of thanks would be greatly appreciated.

Ask a question
Jean-François Pillou

CCM is a leading international tech website. Our content is written in collaboration with IT experts, under the direction of Jeff Pillou, founder of CCM.net. CCM reaches more than 50 million unique visitors per month and is available in 11 languages.

Learn more about the CCM team

Published by . Latest update on 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 (https://ccm.net/).

2 Comments