Java Program to Generate Random Hexadecimal Bytes
To generate Random Hexadecimal Bytes, first, a random byte can be generated in decimal form using Java.util.Random.nextInt() and then it can be converted to hexadecimal form using Integer.toHexString() method.
The nextInt() method is used to obtain the next integer from this random number generator’s sequence. Here the range can also be specified which will return a number between 0 inclusive and the specified number exclusive.
public int nextInt()
Return Value: The method call returns the next integer number from the random number generator sequence
// Here printing n is a random integer. int n = ran.nextInt();
toHexString() is a built-in method in Java which returns a string representation of the integer argument as an unsigned integer in base 16. The function takes a single parameter as an argument in Integer data-type.
public static String toHexString(int num)
Return value: It returns string representation of the integer argument as an unsigned int in base 16
Input:13 Output:d Input:14 Output:e
Random Hexadecimal Byte: 61fdc065
Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more, please refer Complete Interview Preparation Course.