Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

UUID is divided into several versions

2025-03-30 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly explains "UUID is divided into several versions", interested friends may wish to have a look. The method introduced in this paper is simple, fast and practical. Next, let the editor take you to learn "UUID is divided into several versions"!

Universal unique Identification number (English: Universally Unique Identifier, abbreviation: UUID) is a 128-bit identifier used in a computer architecture to identify the number of information, and a related term: global unique identifier (GUID).

Generated according to the standard method and independent of the registration and distribution of the central authority, UUID is unique, unlike most other numbering schemes. The probability of repeated UUID codes is close to zero and can be ignored.

UUID consists of a set of 32-digit hexadecimal digits, so the theoretical total of UUID is 16 ^ 32 = 2 ^ 128, which is approximately 3.4 x 10 ^ 38. In other words, if you generate 1 trillion UUID per nanosecond (ns), it will take 10 billion years to use up all the UUID.

The standard form of UUID consists of 32 hexadecimal digits, divided into five segments by hyphens, and 32 characters in the form of 8-4-4-4-12.

Example: 550e8400-e29b-41d4-a716-446655440000

UUID format:

In its canonical text representation, the 16 8-bit bytes of UUID are represented as 32 hexadecimal (radix 16) digits, showing that "8-4-4-4-12" has a total of 36 characters (32 alphanumeric characters and 4 hyphens) in five groups separated by hyphens. For example:

123e4567-e89b-12d3-a456-426655440000 xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxxps: compared up and down, a stands for 10 in hexadecimal

The four-digit M represents the UUID version, and the one to three most significant digits of the number N represent the UUID variant. In the example, M is 1 and N is a (10xx), which means that this UUID is "variant 1" and "version 1" UUID; is time-based DCE/RFC 4122 UUID.

UUID version:

For "variant (variants) 1" and "variant 2", five versions (versions) are defined in the standard, and each version may be more appropriate than the others in a particular use case.

The version is indicated in the M string.

Version 1-UUID is generated based on time and node ID (usually MAC address)

Version 2-UUID is generated based on identifiers (usually group or user ID), time, and node ID

Version 3, version 5-deterministic UUID is generated from hashing namespace (namespace) identifiers and names

Version 4-UUID is generated using randomness or pseudo-randomness.

Generate UUID in Java:

Let's take a look at the methods that can be used to generate UUID in the Java UUID class:

1. RandomUUID ():

It uses a pseudo-random number generator with high encryption strength to generate v4 pseudo-random UUID:

UUID uuid = UUID.randomUUID (); 2. NameUUIDFromBytes ():

We can use nameuidfrombytes () to generate UUID from a byte array:

Byte [] byteArr = {11,23,33}; UUID uuid = UUID.nameUUIDFromBytes (byteArr)

This method generates v3 UUID (name-based).

3. FromString ():

Using fromString (), we can create a UUID from a standard string representation:

UUID uuid = UUID.fromString ("533a4559-e55c-18b3-2456-555563322002")

It throws an IllegalArgumentException for any invalid string passed as an argument.

Compare the two uuid:

Java UUID implements the Comparable interface, so we can use the compareTo () method to compare them

UUID uuid1 = UUID.randomUUID (); UUID uuid2 = UUID.randomUUID (); int```result = uuid1.compareTo (uuid2)

As we know, the compareTo () method returns:

1: if uuid1 is greater than uuid2

0: if uuid1=uuid2

-1: if uuid1 is less than uuid2

We can also choose to use the equals () method for comparison.

At this point, I believe you have a deeper understanding of "UUID is divided into several versions". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 262

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report