ãã¨ãã°ãIPã¢ãã¬ã¹ï¼IPv4ï¼ã¯32ãããã§ã32ãããå
ããããã¯ã¼ã¯ã¢ãã¬ã¹ã¨ãã·ã³ã¢ãã¬ã¹ã«ãªã£ã¦ãã¾ãã
ããã¦ãIPã¢ãã¬ã¹ããµãããããã¹ã¯ããã¨ãããã¯ã¼ã¯ã¢ãã¬ã¹ãæ±ã¾ãã¾ããã
ã¤ã¾ããä»»æã®ãã·ã³ããã©ã®ãããã¯ã¼ã¯ã¢ãã¬ã¹ã«å±ãããã¯ãIPã¢ãã¬ã¹ããµãããããã¹ã¯ã§è«çç©ããã°ãåããã®ã§ãã
- '1'ã®æåã¯16é²ã§ã¯ã31ãã§2é²æ°ã§ã¯ã00110001ãã§ãã
- '0'ã®æåã¯16é²ã§ã¯ã30ãã§2é²æ°ã§ã¯ã00110000ãã§ãã
- æå1ã00110001ãã¨æå0ã00110000ãã®è«çç©ã¯ãæå0ã00110000ãã«ãªãã¾ãã
- æå1ã00110001ãã¨æå1ã00110001ãã®è«çç©ã¯ãæå1ã00110001ãã«ãªãã¾ãã
ä¸æ¹ãPerlã§ã¯ãããæ¼ç®åã®è«çç©ã&ããããã¾ããã
1ã¨0ã®æåã®è«çç©ã®å ´åã¯ãæååã§ããããæ¼ç®åã®è«çç©ã&ãã使ããã®ã§ãï¼
ãã¨ãã°ããµãããããã¹ã¯255.255.255.0ã®IPã¢ãã¬ã¹172.11.22.33ã®ãããã¯ã¼ã¯ã¢ãã¬ã¹ã¯ã172.11.22.0ã§ããã
ãããPerlã§è¨ç®ããã¨ä»¥ä¸ã®æãã«ãªãã¾ãã
use strict; use warnings; # IPã¢ãã¬ã¹ã®ãããã¢ãã¬ã¹ããããåã®æååã¸å¤æ print dec2bin4IP('255.255.255.0'), "\n"; # IPã¢ãã¬ã¹ã®ãããåã®æååããããã¢ãã¬ã¹ã¸å¤æ print bin2dec4IP(dec2bin4IP('255.255.255.0')), "\n"; # ãã¹ã¯ãã¦ãããã¯ã¼ã¯ã¢ãã¬ã¹ãæ±ãã print bin2dec4IP(dec2bin4IP('255.255.255.0') & dec2bin4IP('172.11.22.33')), "\n"; sub dec2bin4IP { my ($ip) = @_; my @aaa = split(/\./, $ip); my $bin = ''; for (my $i = 0; $i < 4; $i++) { $bin .= unpack("B8", pack("C", $aaa[$i])); } return $bin; } sub bin2dec4IP { my ($ip) = @_; $ip =~ /(........)(........)(........)(........)/; my $num1 = unpack("C", pack("B8",$1)); my $num2 = unpack("C", pack("B8",$2)); my $num3 = unpack("C", pack("B8",$3)); my $num4 = unpack("C", pack("B8",$4)); return "$num1.$num2.$num3.$num4"; }
- dec2bin4IPã¯ãIPã¢ãã¬ã¹ã®ãããã¢ãã¬ã¹ããããåã®æååã¸å¤æãã¾ãã
- bin2dec4IPã¯ãIPã¢ãã¬ã¹ã®ãããåã®æååããããã¢ãã¬ã¹ã¸å¤æãã¾ãã
ä¸è¨ã¯ããµãããããã¹ã¯ã¨IPã¢ãã¬ã¹ããããåã®æååã¸å¤æãã¦ããã®è«çç©ãã¨ã£ã¦ããããã¯ã¼ã¯ã¢ãã¬ã¹ãåå¾ãããããåã®æååãããããã¢ãã¬ã¹ã¸å¤æãã¦ãã¾ãã
ããã¦ããã®å¤æå¤ãã¤ã¾ãããããã¯ã¼ã¯ã¢ãã¬ã¹ 172.11.22.0 ã表示ãã¦ãã¾ãã
å°ãpackã¯ãå¼æ°ããã¤ããªå¤ã«å¤æããunpackã¯ããã¤ããªå¤ããå ã®å¤ã«å¤æãã¾ãã
- pack("C", æåå)ã¯ãæååããã¤ããªå¤ã«å¤æ
- unpack("B8", ãã¤ããªå¤)ã¯ããã¤ããªå¤ãããã8æåã®æååã«å¤æ
- pack("B8", ããã8æåã®æåå)ã¯ãããã8æåã®æååããã¤ããªå¤ã«å¤æ
- unpack("C", ãã¤ããªå¤)ã¯ããã¤ããªå¤ãæååã«å¤æ
追è¨
kitsããããããã£ã¨ã¹ãã¼ããªæ¹æ³ãæãã¦é ãã¾ããï¼ããã¯ãã¼ã¯ã§â¦ï¼ã
my $ip = '255.255.255.0'; my $bin = sprintf "%08b"x4, split /\./, $ip; print $bin, "\n"; print join '.', map {oct "0b$_"} $bin=~/(\d{8})(\d{8})(\d{8})(\d{8})/;
ã$bin=~/(\d{8})(\d{8})(\d{8})(\d{8})/ãã¯ãã«ãã³ã使ã£ãè¨æ¶ããªã¹ãã§è¿ãï¼ç¥ããªãã£ãã§ããå¾ããæ°åã§ãï¼ã
ããã"0b$_"ã§æåé£çµãã¦ãoctã§10é²æ°ã«ãã¦joinããã
ã§ããã°ãæ¬ããã°ã®å¿æ´ã¯ãªã㯠ãããããããé¡ãè´ãã¾ãã