SO でヘルプを検索しましたが、質問に対する回答が見つかりませんでした。
状況: 「/NN」サブネット マスク表記 (IPTABLES と考えてください) を 0.0.0.0 cisco 表記に変換する必要があります。
NN は、下位オクテットから上位へのサブマスク内の「1」の数です。各オクテットは 8 ビット整数です。
考えられる解決策:
32 個の「0」の配列を作成し、最後の NN 桁を「1」で埋めてから、4 オクテットにグループ化し、int に変換します... /23 マスクは 0.0.1.255 のようになります。
私の質問は.NETでそれを行う方法です...バイナリ操作と変換を使用したことはありません。
この解決策を手伝ってもらえますか?
更新 - スティーブンは正しく答えました!
.NETに移植されたコードは次のとおりです
if (p.LastIndexOf("/") < 0 ) return p;
int mask= Convert.ToInt32("0"+p.Substring(p.LastIndexOf("/")+1,2));
int zeroBits = 32 - mask; // the number of zero bits
uint result = uint.MaxValue; // all ones
// Shift "cidr" and subtract one to create "cidr" one bits;
// then move them left the number of zero bits.
result &= (uint)((((ulong)0x1 << mascara) - 1) << zeroBits);
result = ~result;
// Note that the result is in host order, so we'd have to convert
// like this before passing to an IPAddress constructor
result = (uint)IPAddress.HostToNetworkOrder((int)result);
string convertedMask = new IPAddress(result).ToString();