Skip to content

Instantly share code, notes, and snippets.

@chochos
Created April 16, 2015 23:44
Show Gist options
  • Save chochos/e93e23e0d6e050d1e41a to your computer and use it in GitHub Desktop.
Save chochos/e93e23e0d6e050d1e41a to your computer and use it in GitHub Desktop.
Encode/Decode 16-bit unsigned integer
byte[] encode(int x) {
byte[] b = new byte[2];
b[0] = (byte)(x>>8 & 0xff);
b[1] = (byte)(x & 0xff);
return b;
}
int decode(byte[] b) {
return ((b[0] & 0xff) << 8) | (b[1] & 0xff);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment