Last active
November 19, 2016 05:40
-
-
Save stknohg/95eab3ece397bb77226f5ead82bc1a9b to your computer and use it in GitHub Desktop.
192.168.100.240/24 →192.168.100.240/255.255.255.0 をfor無しで変換するワンライナー
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# whileありバージョン | |
"192.168.100.240/24"|&{$f,$s=$input-split"/";$f+"/"+($($i=-1;while(3-gt$i++){[Convert]::ToInt32(-join("1"*$s+"0"*(32-$s))[($i*8)..($i*8+7)],2)})-join".")} | |
# テストコード | |
0..32 | % { "192.168.100.240/$_" } | % { | |
$_ | &{$f,$s=$input-split"/";$f+"/"+($($i=-1;while(3-gt$i++){[Convert]::ToInt32(-join("1"*$s+"0"*(32-$s))[($i*8)..($i*8+7)],2)})-join".")} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# whileなしバージョン | |
"192.168.100.240/24"|&{$f,$s=$input-split"/";"$f/$([Array]::ConvertAll(0..3,[Converter[int,int]]{param($i)[Convert]::ToInt32(-join("1"*$s+"0"*(32-$s))[($i*8)..($i*8+7)],2)})-join".")"} | |
# テストコード | |
0..32 | % { "192.168.100.240/$_" } | % { | |
$_ | &{$f,$s=$input-split"/";"$f/$([Array]::ConvertAll(0..3,[Converter[int,int]]{param($i)[Convert]::ToInt32(-join("1"*$s+"0"*(32-$s))[($i*8)..($i*8+7)],2)})-join".")"} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 3.0 | |
"192.168.100.240/24"|&{$f,$s=$input-split"/";"$f/$([BitConverter]::GetBytes($(if([int]$s){0xFFFFFFFF-shl(32-$s)}else{0}))[3..0]-join".")"} | |
# テストコード | |
0..32 | % { "192.168.100.240/$_" } | % { | |
$_ | &{$f,$s=$input-split"/";"$f/$([BitConverter]::GetBytes($(if([int]$s){0xFFFFFFFF-shl(32-$s)}else{0}))[3..0]-join".")"} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 3.0 | |
# zuckie (@basso414f)さんによる改良版 | |
"192.168.100.240/24"|&{$f,$s=$input-split"/";"$f/$([BitConverter]::GetBytes(-1l-shl(32-$s))[3..0]-join".")"} | |
# テストコード | |
0..32 | % { "192.168.100.240/$_" } | % { | |
$_ | &{$f,$s=$input-split"/";"$f/$([BitConverter]::GetBytes(-1l-shl(32-$s))[3..0]-join".")"} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
検証環境は64bit版 Windows10 PowerShell 5.1。