diff --git a/README.md b/README.md new file mode 100644 index 0000000..169345f --- /dev/null +++ b/README.md @@ -0,0 +1,24 @@ +# Netcalc # +This is a simple CLI network calculator made in PHP. Usage is pretty straight +forward. For example, let say you want to know how many usable addresses you +will get with a 26 bit netmask, simply run the script as below: + + php netcalc.php 26 + +The above command will give you the following outut: + + Netmask + ------- + In slash notation: 26 + In dotted decimal: 255.255.255.192 + In dotted binary: 11111111.11111111.11111111.11000000 + + Network size + ------------ + Total number of addresses: 64 + Number of usable addresses for hosts: 62 + +## Copyright ## +Netcalc is written by Jack-Benny Persson and released under GNU GPL version 2. + + diff --git a/netcalc.php b/netcalc.php index 5167b70..524c4c3 100644 --- a/netcalc.php +++ b/netcalc.php @@ -59,6 +59,11 @@ function print_netmask($number) // Put it together in a binary string $binary = "$ones$zeros"; + // Make it dotted binary format + $dotted_binary = substr_replace($binary, ".", 8, 0); + $dotted_binary = substr_replace($dotted_binary, ".", 17, 0); + $dotted_binary = substr_replace($dotted_binary, ".", 26, 0); + // Make it dotted decimal format $firstOctet = base_convert(substr($binary, 0, 8), 2, 10); $secondOctet = base_convert(substr($binary, 8, 8), 2, 10); @@ -69,9 +74,8 @@ function print_netmask($number) print "Netmask\n" ; print "-------\n"; print "In slash notation: $mask\n"; - print "In binary: "; - print $binary . "\n"; - print "In dotted decimal: $firstOctet.$secondOctet.$thirdOctet.$forthOctet"; + print "In dotted decimal: $firstOctet.$secondOctet.$thirdOctet.$forthOctet\n"; + print "In dotted binary: $dotted_binary"; print "\n\n"; }