Skip to content

Instantly share code, notes, and snippets.

@luckyhandler
Last active September 26, 2016 19:42
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save luckyhandler/ccefc79095febf61fa5e0a6d1a01e39f to your computer and use it in GitHub Desktop.
Save luckyhandler/ccefc79095febf61fa5e0a6d1a01e39f to your computer and use it in GitHub Desktop.
Java - int to byte[] / byte[] to int
static byte[] mapPayload(int toConvert) {
byte[] ret = new byte[4];
ret[3] = (byte) (toConvert & 0xFF);
ret[2] = (byte) ((toConvert >> 8) & 0xFF);
ret[1] = (byte) ((toConvert >> 16) & 0xFF);
ret[0] = (byte) ((toConvert >> 24) & 0xFF);
return ret;
}
static int unmapPayload(byte[] payload) {
return (payload[3] & 0xFF) +
((payload[2] & 0xFF) << 8) +
((payload[1] & 0xFF) << 16) +
((payload[0] & 0xFF) << 24);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment