How do I convert an ip address into an array of bytes?
Use the getAddress() method in the InetAddress class. InetAddress address = InetAddress.getByName("192.168.1.1"); byte[] bytes = address.getAddress(); for (byte b : bytes) { System.out.println(b &...
View ArticleHow to convert a hex string to a byte array?
The BigDecimal class makes this really easy as it has a method toByteArray() that returns exactly what we need. Just need to create a BigDecimal and call the method. byte[] bytes = new...
View ArticleHow to convert a long to a byte array
The following code can be used to extract the 8 bytes from a long value and return them as a byte array public static byte[] longToByteArray(long data) { return new byte[] { (byte)((data >> 56)...
View ArticleHow to save your SecretKey?
Once you have generated a SecretKey you can use it’s getEncoded() method to generate a byte array that can be used to recreate the key. // Use the following to initially create a random key...
View ArticleHow to convert byte array to long
The following code can be used to convert a byte array (containing the bytes of a long) into a long. public static long byteArrayToLong(byte[] bytes) { long l = 0; for (int i=0; i<8; i++) { l...
View Article
More Pages to Explore .....