Skip to content

Instantly share code, notes, and snippets.

@DBarthe
Last active March 17, 2017 18:49
Show Gist options
  • Save DBarthe/83ada33119ff52ab99773bf3f92d075e to your computer and use it in GitHub Desktop.
Save DBarthe/83ada33119ff52ab99773bf3f92d075e to your computer and use it in GitHub Desktop.
class Demonstration {
public void getIp(byte[] packet) {
// disons que l'ip commence à packet[0]
int w = packet[0] & 0xFF; // premier octet
int x = packet[1] & 0xFF; // deuxieme octet
int y = packet[2] & 0xFF; // troisieme...
int z = packet[3] & 0xFF; // quatrieme ...
// le '& 0xFF' c'est pour que la convertion byte -> int se fasse comme on l'attend
// Pas besoin de cast ducoup
int ip = z | (y << 8) | (x << 16) | (w << 24);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment