Share the post "Create Random String Using Hex String In Java"
Here is a short method to create a random string using a class, in this case the Integer class’, toHexString() method to generate random hex strings from at least a specified length. These are rounded up to a multiple of 8 characters and this is assuming you are flexible with the character set that will be randomly generated.
|
1 2 3 4 5 6 7 8 |
public static String createRandomString(int length) { Random random = new Random(); StringBuilder sb = new StringBuilder(); while (sb.length() < length) { sb.append(Integer.toHexString(random.nextInt())); } return sb.toString(); } |