Skip to content

Instantly share code, notes, and snippets.

@nulpatrol
Created July 28, 2015 06:31
Show Gist options
  • Save nulpatrol/29074dafb1891e814779 to your computer and use it in GitHub Desktop.
Save nulpatrol/29074dafb1891e814779 to your computer and use it in GitHub Desktop.
Pack and unpack IP address (InetAddress) to/from int
public static int pack(byte[] bytes) {
int val = 0;
for (byte oneByte : bytes) {
val <<= 8;
val |= oneByte & 0xff;
}
return val;
}
public static short[] unpack(int bytes) {
return new short[] {
(short)((bytes >>> 24) & 0xff),
(short)((bytes >>> 16) & 0xff),
(short)((bytes >>> 8) & 0xff),
(short)((bytes ) & 0xff)
};
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment