Create UUID with zeros
Asked Answered
K

4

47

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.

Karlynkarma answered 30/12, 2013 at 13:9 Comment(4)
I believe (and hope) than an all zeros UUID is invalid! Otherwise uuid_is_null(3) won't make sense.Surmise
As the error says, that's not a valid UUID.Subsocial
@BasileStarynkevitch, I want it to be either "MinValue" or "Invalid".Karlynkarma
IllegalArgumentException - If name does not conform to the string representation as described in toString() So check toString() and see what is not correct.Pericles
C
101

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

Civies answered 30/12, 2013 at 13:26 Comment(0)
P
16

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

Pirri answered 30/12, 2013 at 13:12 Comment(3)
Yes, you can try it here: tryjava8.com/app/snippets/52c17184e4b00bdc99e8a92fBayou
May i know why is this format used? Is it based on some spec or standard?Thrive
they are indeed.Fleda
F
4

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.

Familiarity answered 31/7, 2018 at 21:13 Comment(0)
H
1

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
Homothermal answered 6/12, 2023 at 18:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.