Last active
September 26, 2016 19:42
-
-
Save luckyhandler/ccefc79095febf61fa5e0a6d1a01e39f to your computer and use it in GitHub Desktop.
Java - int to byte[] / byte[] to int
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
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