0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【C#】warning CS0618: 'IPAddress.Address' is obsolete: が出たとき

Posted at

結論

IPAddress.Adderessは非推奨のため、IPAddress.GetAddressBytes();を使おうというものです。

private static long NewIPAddress(IPAddress p)
        {
            byte[] addressBytes = p.GetAddressBytes();
            if (addressBytes.Length != 4)
            {
                throw new ArgumentException("Must be an IPv4 address");
            }
            int networkOrder = BitConverter.ToInt32(addressBytes, 0);
            return networkOrder;
            //ネットワークを経由するなら下の行を入れて反転させる必要がある
            //return (uint)IPAddress.NetworkToHostOrder(networkOrder); 
            
        }

背景

業務で扱っているアプリケーションの修正中に、ビルドするとこのWarningがあったので調べました。Addressをそのまま返していましたが、byte型の配列としてIPアドレスを取得して、int32型に変換しています。自分が扱ったアプリケーションではこの関数を用いた部分はネットワークを経由していないため順番を入れ替えていませんが、ネットワークを経由するならエンディアンを考える必要があります。よって、返り値はNetworkToHostOrder()かHostToNetworkOrder()を使ってエンディアンを変える必要があります。

エンディアンとは

リトルエンディアンとビッグエンディアンがあります。データのビット列を格納するときに、そのままの順番で格納するのがビッグエンディアン、反転して入れるのがリトルエンディアンです。ビッグエンディアンは人間が考えるときに分かりやすいです。リトルエンディアンは、バイト毎の並びを256進法であるとして見た場合に、アドレス+0 にあるのが$256^0$の桁、アドレス+1 にあるのが $256^1$ の桁と数値の並びに整合性があることと、多倍長加算などで最下位から順番に計算したほうが繰上りの計算がしやすいことに利益がある。現在ではネットワーク機器やCPUでエンディアンが異なっている場合があり、その場合は処理中にエンディアンを変換する必要があります。

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?