Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 991
Yaroslav Avatar asked Jul 29 '17 10:07

Yaroslav


People also ask

What is the length of UUID in Java?

A UUID is made up of hex digits (4 chars each) along with 4 “-” symbols, which make its length equal to 36 characters. The Nil UUID is a special form of UUID in which all bits are set to zero.

How do you reduce the length of a UUID generated using randomUUID?

You can use base64 encoding and reduce it to 22 characters. If you use base94 you can get it does to 20 characters. If you use the whole range of valid chars fro \u0000 to \ufffd you can reduce it to just 9 characters or 17 bytes.

What is the length of UUID?

What is a UUID. Universally Unique Identifiers, or UUIDS, are 128 bit numbers, composed of 16 octets and represented as 32 base-16 characters, that can be used to identify information across a computer system.

What is UUID randomUUID () in Java?

The randomUUID() method is used to retrieve a type 4 (pseudo randomly generated) UUID. The UUID is generated using a cryptographically strong pseudo random number generator.


2 Answers

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 of generating UUID. Here type 4 means this uuid is generated using a random or pseudo-random number. From wiki - Universally_unique_identifier#Versions:

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.

like image 96
MD Ruhul Amin Avatar answered Oct 08 '22 17:10

MD Ruhul Amin


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

like image 45
Daniel Almeida Avatar answered Oct 08 '22 19:10

Daniel Almeida