Incerteza
Incerteza

Reputation: 34924

Create UUID with zeros

I'm trying to generate a UUID with all zeros:

java.util.UUID fromString "00000000-00000000-00000000-00000000"

The error is

 java.lang.IllegalArgumentException: Invalid UUID string: 00000000-00000000-00000000-00000000
    at java.util.UUID.fromString(UUID.java:194)

What am I doing wrong?

I want to create either "MinValue" or "Invalid" UUID.

Upvotes: 51

Views: 72436

Answers (4)

FlaskDev
FlaskDev

Reputation: 65

UUIDs are standardized identifiers with various versions. UUID version 4 (UUIDv4) adheres to a specific format and is composed of 32 hexadecimal digits separated into five groups in the pattern 8-4-4-4-12, totaling 36 characters, including hyphens.

To validate a UUIDv4 manually, there are specific checks you can perform on the input string:

  1. Length Check: Ensure the UUID string length is 36 characters.
  2. Positional Checks: Confirm the positions of hyphens in the string at indices 8, 13, 18, and 23.
  3. Version Identification: UUIDv4 contains a '4' at the 15th position.
  4. Content Check: The UUIDv4 should contain at least one of '8', '9', 'a', or 'b' at the 20th position.

The UUID version 4 with zeros could be:

  • 00000000-0000-4000-8000-000000000000
  • 00000000-0000-4000-9000-000000000000
  • 00000000-0000-4000-a000-000000000000
  • 00000000-0000-4000-b000-000000000000

Upvotes: 3

StephenS
StephenS

Reputation: 2156

From https://en.wikipedia.org/wiki/Universally_unique_identifier#Nil_UUID:

The "nil" UUID, a special case, is the UUID, 00000000-0000-0000-0000-000000000000; that is, all bits set to zero.

The dashes should follow the normal 8-4-4-4-12 format because that's what the standards say to use and many (most?) tools enforce that on input.

Some tools may accept other formats, e.g. 32 hex digits with no dashes, because they just remove the dashes (if present) before validation anyway, but the particular tool you're using is a bit stricter/smarter, which shows that using non-standard formats is a bad habit that will end up biting you.

Upvotes: 5

Evgeniy Dorofeev
Evgeniy Dorofeev

Reputation: 136062

try this

System.out.println(new UUID(0,0));

it prints

00000000-0000-0000-0000-000000000000

this is the right format to use in UUID.fromString

Upvotes: 108

Craig Moore
Craig Moore

Reputation: 1091

Isn't it supposed to be 8-4-4-4-12? like this: 00000000-0000-0000-0000-000000000000

Upvotes: 16

Related Questions