Java.util.UUID.randomUUID (). toString () length

Is the length used java.util.UUID.randomUUID (). is toString () always equal to 36?

I could not find information about this. It says only the following:

public static UUID randomUUID () Static factory for receiving type 4 UUIDs (pseudo-randomly generated). UUIDs are generated using a cryptographically strong pseudo random number generator. Returns: Randomly Generated UUID

And that type 4 doesn't tell me anything. I do not know what type 4 means in this case.

+24
java string uuid random string-length
source share
3 answers

Is java.util.UUID.randomUUID () always. ToString () length is 36?

Yes!! this is.

UUID is actually a 128-bit value (2 long). To represent 128 bits in a hexadecimal string, there will be 128/4 128/4 128/4=32 characters (each character has a length of 4 bits). In string format, it also contains 4 ( - ), so the length is 36.

example: 54947df8-0e9e-4471-a2f9-9af509fb5889

32 hexadecimal characters + 4 hyphens = 36 characters So the length will always be the same.


Update:

I do not know what type 4 in the case means.

For your information: there are several ways to generate UUIDs. Here, type 4 means that this uuid is generated using a random or pseudo-random number. From the wiki - Universally_unique_identifier # Versions :

Versions

For both options 1 and 2, five β€œversions” are defined in the standards, and each version may be more suitable than the others in specific use cases. The version is indicated by the letter M in a string representation.

Version 1 UUIDs are generated from the time and host ID (usually a MAC address);

Version 2 UUIDs are generated from an identifier (usually a group or user identifier), time, and a node identifier;

versions 3 and 5 create deterministic UUIDs generated by hashing the identifier and namespace name;

and UUID version 4 are generated using a random or pseudo-random number.

+42
source share

You can convert a 16-byte UUIDv4 binary to a 24-byte ascii using base64, instead encode into ascii-hex (32 bytes)

+2
source share

For people like me who start googling before reading Javadoc, here is Javadoc;)

UUID.toString

For those who do not know how to read the grammar tree, read from bottom to top.
hexDigit is one character
hexOctet is 2 hexDigits = 2 characters
node 6 * hexOctet = 6 * 2hexdigit = 6 * 2 characters = 12 characters
_and_sequence option : 2 * hexOctet = 2 * 2hexdigit = 2 * 2 characters = 4 characters
time_high_and_version - 2 * hexOctet = 2 * 2hexdigit = 2 * 2 characters = 4 characters
time_mid is 2 * hexOctet = 2 * 2hexdigit = 2 * 2 characters = 4 characters
time_low - 4 * hexOctet = 4 * 2hexdigit = 4 * 2 characters = 8 characters
and finally, the UUID is <time_low> "-" <time_mid> "-" <time_high_and_version> "-" <variable_and_sequence> "-" <node>

= 8 characters + 1 character + 4 characters + 1 character + 4 characters + 1 character + 4 characters + 1 character + 12 characters

= 36 characters! 128 data bits + 4 hyphens as previously indicated

 The UUID string representation is as described by this BNF: UUID = <time_low> "-" <time_mid> "-" <time_high_and_version> "-" <variant_and_sequence> "-" <node> time_low = 4*<hexOctet> time_mid = 2*<hexOctet> time_high_and_version = 2*<hexOctet> variant_and_sequence = 2*<hexOctet> node = 6*<hexOctet> hexOctet = <hexDigit><hexDigit> hexDigit = "0" | "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" | "a" | "b" | "c" | "d" | "e" | "f" | "A" | "B" | "C" | "D" | "E" | "F" 
0
source share

All Articles