192 x 256^3 + 168 x 256^2 + 1 x 256^1 + 1 x 1
3221225472 + 11010048 + 256 + 1 = 3232235777
http://3232235777Deux manières pour exprimer l'adresse IP en un nombre décimal:
![]() |
Exemple de calcul de (1 x 256^1) en binaire |
public class IP_Decimale {Sortie
public static void main(String[] zero) {
String ip = "192.168.1.1";
//l'adresse IP en décimal
long iplong = IPenBase10(ip);
System.out.println(iplong);
}
public static long IPenBase10(String adresseIP) {
long decimal = 0;
//les nombres sont séparés par des points
//mettre chacun dans une case du tableau
String[] tableauIP = adresseIP.split("\\.");
//de la droite vers la gauche
for (int i = 3; i >= 0; i--) {
//convertir en long
long ip = Long.parseLong(tableauIP[3 - i]);
//décaler de (i*8) bits à gauche et faire la somme
long bitshift= ip << (i * 8);
decimal += bitshift;
System.out.println(ip+" << "+(i*8)+" : "+bitshift);
}
return decimal;
}
}
192 << 24 : 3221225472
168 << 16 : 11010048
1 << 8 : 256
1 << 0 : 1
résulat: 3232235777
public static long IPenBase10puiss(String adresseIP) {Références:
long decimal = 0;
String[] tableauIP = adresseIP.split("\\.");
//de la droite vers la gauche
for (int i = 0; i<=3; i++) {
long ip = Long.parseLong(tableauIP[i]);
long puiss= ip * (long) Math.pow(256, 3-i);
decimal += puiss;
System.out.println(tableauIP[i] +" : "+puiss);
}
return decimal;
}
Please disable your ad blocker and refresh the window to use this website.