Created
April 16, 2015 23:44
-
-
Save chochos/e93e23e0d6e050d1e41a to your computer and use it in GitHub Desktop.
Encode/Decode 16-bit unsigned integer
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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