java.util.UUID.randomUUID().toString() length
Asked Answered
Y

3

66

Does java.util.UUID.randomUUID().toString() length always equal to 36?

I was not able to find info on that. Here it is said only the following:

public static UUID randomUUID() Static factory to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator. Returns: A randomly generated UUID

And that type 4 tells me nothing. I do not know what type 4 means in the case.

Yellowlegs answered 29/7, 2017 at 10:13 Comment(3)
Since UUIDs have a defined length, yes.Scull
What part of 'a UUID represents a 128-bit value' did you fail to understand?Mongeau
Here is what the Javadoc says about types: The version field holds a value that describes the type of this UUID. There are four different basic types of UUIDs: time-based, DCE security, name-based, and randomly generated UUIDs. These types have a version value of 1, 2, 3 and 4, respectively.Scull
G
90

Does java.util.UUID.randomUUID().toString() length always equal to 36?

Yes!! it is.

A UUID actually a 128 bit value (2 long). To represent 128 bit into hex string there will be 128/4=32 char (each char is 4bit long). In string format it also contains 4 (-) that's why the length is 36.

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

32 hex char + 4 hyphen char = 36 char. So the length will be always same.


#Update:

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

FYI: There are several ways to generate UUID. Here type-4 means this uuid is generated using a random or pseudo-random number. From wiki - Universally_unique_identifier#Versions:

UUID Versions

For both variants 1 and 2, five "versions" are defined in the standards, and each version may be more appropriate than the others in specific use cases. Version is indicated by the M in the string representation.

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

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

versions 3 and 5 produce deterministic UUIDs generated by hashing a namespace identifier and name;

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

Goner answered 29/7, 2017 at 10:16 Comment(3)
So, it's always 32 alphanumeric characters plus 4 hyphens?Lancastrian
Yes it is. 36 char long string.Goner
"32 alphanumeric characters" is not actually correct. It should be 32 hexOctet and 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".Goner
H
5

You may convert UUIDv4 16 bytes binary to 24 bytes ascii using base64, instead encode to ascii-hex (32 bytes)

Hovercraft answered 12/12, 2018 at 9:14 Comment(2)
try to highlight the keywords and be clear with the format it will help to reach out your answer for othersCoolie
That's a nice idea, for instance I have to integrate a UUID-v4-based identifiers system with another system which limits IDs to 30 charactes max length.Balf
J
4

For those like me that start googling before reading the javadoc, here the javadoc ;)

UUID.toString

For those that Don't know how to read a grammar tree read from Bottom to Top.
an hexDigit is one char
an hexOctet is 2 hexDigits = 2chars
a node is 6 * hexOctet = 6 * 2hexdigit = 6*2 chars = 12chars
a variant_and_sequence is 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_high_and_version is 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_mid is 2 * hexOctet = 2 * 2hexdigit = 2*2 chars = 4chars
a time_low is 4 * hexOctet = 4* 2hexdigit = 4*2 chars = 8chars
and finaly, a UUID is < time_low > "-" < time_mid > "-" < time_high_and_version > "-" < variant_and_sequence > "-"< node >

= 8 chars + 1 char + 4 chars + 1 char + 4 chars + 1 char + 4 chars + 1 char + 12 chars

= 36 chars ! 128 bit of data + 4 hyphen as stated previously

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"
Joeannjoed answered 18/5, 2019 at 8:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.