Java – Convert byte[] to int and vice versa

In Java, we can use ByteBuffer to convert int to byte[] and vice versa. int to byte[] int num = 1; // int need 4 bytes, default ByteOrder.BIG_ENDIAN byte[] result = ByteBuffer.allocate(4).putInt(number).array(); byte[] to int byte[] byteArray = new byte[] {00, 00, 00, 01}; int num = ByteBuffer.wrap(bytes).getInt(); 1. int to byte[] This Java example …

Read more

Java – How to convert byte arrays to Hex

This article shows you a few ways to convert byte arrays or byte[] to a hexadecimal (base 16 or hex) string representative. String.format Integer.toHexString Apache Commons Codec – commons-codec Spring Security Crypto – spring-security-crypto Bitwise shifting and masking. (educational purposes) Note Both Apache Commons-Codec and Spring Security Crypto modules are using the similar 5. Bitwise …

Read more