私は最近、IPアドレスを小数点、Octal、および16進数に変換することもWebサイトにアクセスできることを学びました。
IPをデジタルに変換します(2番目のアルゴリズム。左シフト、ビットワイズ、または実装を使用します。より効率的です。):
public long iptolong(string ipaddress){long result = 0; string [] ipaddressinarray = ipaddress.split( "//。"); for(int i = 3; i> = 0; i-){long ip = long.parselong(ipaddressinarray [3-i]); //左24,16,8,0およびビットワイズまたは// 1。 192 << 24 // 1。 168 << 16 // 1。 1 << 8 // 1。 2 << 0 result | = ip <<(i * 8); } return result; }数字がIPに変換されると、両方のアルゴリズムが類似しています。
// ip = 3232235778 public string longtoip(long ip){stringbuilder result = new StringBuilder(15); for(int i = 0; i <4; i ++){result.insert(0、long.tostring(ip&0xff)); if(i <3){result.insert(0、 '。'); } ip = ip >> 8; } return result.toString(); } // ip = 3232235778 public string longtoip2(long ip){return((ip >> 24)&0xff) + "。" +((ip >> 16)&0xff) + "。" +((ip >> 8)&0xff) + "。" +(ip&0xff); }完全なコード:
パブリッククラスjavabitwiseexample {public static void main(string [] args){javabitwiseexample obj = new javabitwiseexample(); system.out.println( "iptolong:" + obj.iptolong( "192.168.1.2")); System.out.println( "iptolong2:" + obj.iptolong2( "192.168.1.2")); System.out.println( "longtoip:" + obj.longtoip(3232235778l)); System.out.println( "longtoip2:" + obj.longtoip2(3232235778l)); } //例:192.168.1.2 public long iptolong(string iPaddress){// iPaddressinArray [0] = 192 String [] iPaddressinArray = iPaddress.split( "//。");長い結果= 0; for(int i = 0; i <ipaddressinarray.length; i ++){int power = 3 -i; int ip = integer.parseint(ipaddressinarray [i]); //1。192 * 256^3 //2。168 * 256^2 //3。1 * 256^1 //4。2 * 256^0 result += ip * math.pow(256、power); } return result; } public long iptolong2(string ipaddress){long result = 0; string [] ipaddressinarray = ipaddress.split( "//。"); for(int i = 3; i> = 0; i-){long ip = long.parselong(ipaddressinarray [3-i]); //左シフト24,16,8,0およびビットワイズまたは//1。192<< 24 //1。168<< 16 //1。1<< 8 //1。2<< 0 result | = ip <<(i * 8); } return result; } public string longtoip(long i){return((i >> 24)&0xff) + "。" +((i >> 16)&0xff) + "。" +((i >> 16)&0xff) + "。" +((i >> 8)&0xff) + "。" +(i&0xff); } public string longtoip2(long ip){stringbuilder sb = new StringBuilder(15); for(int i = 0; i <4; i ++){//1。2//2。1//3。168//4。192sb.insert(0、long.toString(ip&0xff)); if(i <3){sb.insert(0、 '。'); } //1。192.168.1.2//2。192.168.1//3。192.168//4。192ip = ip >> 8; } return sb.toString(); }}要約します
上記は、JavaプログラミングIPアドレスと番号のコード変換例に関するこの記事のすべての内容です。私はそれが誰にでも役立つことを願っています。興味のある友人は、このサイトの他の関連トピックを引き続き参照できます。欠点がある場合は、それを指摘するためにメッセージを残してください!