Skip to content

Instantly share code, notes, and snippets.

@ffbit
Last active December 31, 2015 20:49
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 ffbit/8042378 to your computer and use it in GitHub Desktop.
Save ffbit/8042378 to your computer and use it in GitHub Desktop.
int to bytes
import org.junit.Test;
import java.util.Arrays;
public class IntToBitesTest {
@Test
public void intToBytes() throws Exception {
int n = 1234567890;
byte[] a = new byte[4];
System.out.println(Integer.toHexString(n));
for (int i = 0; i < a.length; i++) {
a[a.length - i - 1] = (byte) (n >> (8 * i) & 0xFF);
}
for (byte e : a) {
System.out.print(Integer.toHexString(e & 0xFF) + " ");
}
System.out.println();
System.out.println(Arrays.toString(a));
}
}
499602d2
49 96 2 d2
[73, -106, 2, -46]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment