Recientemente aprendí que convertir direcciones IP en decimal, octal y hexadecimal también puede acceder al sitio web.
Convierta IP en digital (el segundo algoritmo. Use el cambio izquierdo, bit a bit o implementación. Más eficiente).:
public Long Iptolong (String iPaddress) {Long Result = 0; String [] iPaddressInArray = ipaddress.split ("//."); para (int i = 3; i> = 0; i--) {long ip = long.parselong (iPaddressinarray [3-i]); // a la izquierda cambiando 24,16,8,0 y bitwise o // 1. 192 << 24 // 1. 168 << 16 // 1. 1 << 8 // 1. 2 << 0 resultado | = ip << (i * 8); } resultado de retorno; }Cuando los números se convierten a IP, ambos algoritmos son similares:
// 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 resultado.toString (); } // ip = 3232235778 cadena pública longToip2 (ip larga) {return ((ip >> 24) y 0xff) + "." + ((ip >> 16) y 0xff) + "." + ((ip >> 8) y 0xff) + "." + (ip y 0xff); }Código completo:
clase pública javabitwiseExample {public static void main (string [] args) {javabitwiseExample obj = new javabitwiseeExample (); 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 (32322357778l)); } // Ejemplo: 192.168.1.2 public Long iPtolong (String iPaddress) {// iPaddressInArray [0] = 192 String [] iPaddressInArray = ipaddress.split ("//."); resultado largo = 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 Resultado += IP * Math.Pow (256, potencia); } resultado de retorno; } public Long iPtolong2 (String iPaddress) {Long Result = 0; String [] iPaddressInArray = ipaddress.split ("//."); para (int i = 3; i> = 0; i--) {long ip = long.parselong (iPaddressinarray [3-i]); // a la izquierda cambiando 24,16,8,0 y bitwise o // 1. 192 << 24 // 1. 168 << 16 // 1. 1 << 8 // 1. 2 << 0 resultado | = ip << (i * 8); } resultado de retorno; } public String longToip (long i) {return ((i >> 24) y 0xff) + "." + ((i >> 16) y 0xff) + "." + ((i >> 16) y 0xff) + "." + ((i >> 8) y 0xff) + "." + (i & 0xff); } public String LongToip2 (Long IP) {StringBuilder sb = new StringBuilder (15); para (int i = 0; i <4; i ++) {// 1. 2 // 2. 1 // 3. 168 // 4. 192 sb.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. 192 IP = IP >> 8; } return sb.ToString (); }}Resumir
Lo anterior es todo el contenido de este artículo sobre los ejemplos de conversión de código de las direcciones y números IP de programación de Java. Espero que sea útil para todos. Los amigos interesados pueden continuar referiéndose a otros temas relacionados en este sitio. Si hay alguna deficiencia, deje un mensaje para señalarlo.