UUID生成的字符类型 [英] Type of Character generated by UUID

查看:131
本文介绍了UUID生成的字符类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  1. java.util.UUID 是否生成特殊字符?
  2. UUID生成的每个字符的类型是什么(例如,大写,小写,数字).

推荐答案

tl; dr

您问:

java.util.UUID会生成特殊字符吗?

Does java.util.UUID generates special characters?

不.UUID实际上是 128位值,而不是文本.

No. A UUID is actually a 128-bit value, not text.

UUID的文本表示形式通常是一串十六进制数字(0-9,a-f,A-F)加上连字符.

A UUID’s textual representation is canonically a string of hex digits (0-9, a-f, A-F) plus hyphens.

您问:

UUID生成的每个字符的类型是什么(例如,大写,小写,数字).

What are the type of each character (eg- Uppercase, lower case, digits) generated by UUID.

根据UUID规范的要求,十六进制字符串中表示UUID值的所有a至f字符必须全部小写.但是侵权行为比比皆是.

As required by the UUID spec, any a-to-f characters in the hex string representing a UUID value must be in all lowercase. But violations abound.

需要澄清的是, UUID 实际上是 128位值>,不是文字,也不是数字.

To clarify, a UUID is actually a 128-bit value, not text, not digits.

您可以将它们视为128位无符号整数.但是它们实际上不是数字,因为某些位的位置具有语义和特定含义.哪些位具有哪些含义因变体

You could think of them as 128-bit unsigned integers. But they are not actually numbers, as certain bit positions have semantics, specific meanings. Which bits have which meanings varies by variant and by version of UUID.

人类不能很好地读写128位(128个 1 0 字符).当需要编写供人们食用的UUID时,我们使用以16为底的十六进制(数字 0 - 9 和字母 a - f )字符串.我们使用32个十六进制字符和4个连字符分组来表示总共128个字符中的128个位.例如:

Humans don't do well reading and writing 128 bits as 128 1 and 0 characters. When a UUID needs to be written for human consumption, we use a base-16 Hexadecimal (digits 0-9 and letters a-f) string. We use 32 hex characters grouped with 4 hyphens to represent those 128 bits in a total of 36 characters. For example:

550e8400-e29b-41d4-a716-446655440000

550e8400-e29b-41d4-a716-446655440000

没有特殊"字符

对于问题中提到的特殊字符",您只会在UUID的十六进制字符串表示形式中看到这23个可能的字符:

No "Special" Characters

As for "special characters" mentioned in the Question, you will only see these 23 possible characters in a hex-string representation of a UUID:

abcdefABCDEF1234567890-

abcdefABCDEF1234567890-

规范要求的小写

日期为2008-08的最新国际规范(强调我的):

6.5.4生成UUID的十六进制表示形式的软件不得使用大写字母.注–建议将所有人类可读格式中使用的十六进制表示形式限制为小写字母.但是,处理此表示形式的软件需要接受6.5.2中指定的大写和小写字母.

6.5.4 Software generating the hexadecimal representation of a UUID shall not use upper case letters. NOTE – It is recommended that the hexadecimal representation used in all human-readable formats be restricted to lower-case letters. Software processing this representation is, however, required to accept both upper and lower case letters as specified in 6.5.2.

常见违反行为

但是,Microsoft,Apple和其他公司通常违反小写规则.微软曾在某一时刻发布了生成大小写混合(同时使用大写和小写字母)的软件,这显然是一个意外的功能.

Violations Common

However, Microsoft, Apple, and others commonly violate the lowercase rule. At one point Microsoft released software that generated mixed case (using both upper- and lowercase), apparently an unintended feature.

按照规范要求进行操作:

So do as the spec says:

  • 使用小写字母进行输出.
  • 将小写或大写都用作输入.

UUID 类" BNF toString 方法文档>与UUID标准规范相反,生成字符串时允许使用大写字母.但是,该类及其 toString 方法在针对Java 8的Oracle实现是正确的,使用小写字母表示输出,但允许使用大写字母或小写字母作为输入.

The Java documentation for the UUID class’ toString method documents in BNF that uppercase is allowed when generating a string, in contradiction to the UUID standard specification. However the actual behavior of the class and its toString method in the Oracle implementation for Java 8 is correct, using lowercase for output but tolerating either uppercase or lowercase for input.

以小写/大写形式输入:

Input in either lower-/uppercase:

UUID uuidFromLowercase = UUID.fromString ( "897b7f44-1f31-4c95-80cb-bbb43e4dcf05" ); 
UUID uuidFromUppercase = UUID.fromString ( "897B7F44-1F31-4C95-80CB-BBB43E4DCF05" );

仅输出为小写字母:

System.out.println ( "uuidFromLowercase.toString(): " + uuidFromLowercase );
System.out.println ( "uuidFromUppercase.toString(): " + uuidFromUppercase );

uuidFromLowercase.toString():897b7f44-1f31-4c95-80cb-bbb43e4dcf05

uuidFromLowercase.toString(): 897b7f44-1f31-4c95-80cb-bbb43e4dcf05

uuidFromUppercase.toString():897b7f44-1f31-4c95-80cb-bbb43e4dcf05

uuidFromUppercase.toString(): 897b7f44-1f31-4c95-80cb-bbb43e4dcf05

请参见此在IdeOne.com中实时运行的代码.

如果尚未知道UUID,则可以使用由全零组成的特殊UUID.

When the UUID is not yet known, you can use a special UUID consisting of all zeros.

00000000-0000-0000-0000-000000000000

00000000-0000-0000-0000-000000000000

示例值

您可以使用许多生成值的网站中的任何一个来查看一些UUID值示例.例如:

Example Values

You can see some examples of UUID values by using any of the many web sites that generate values. For example:

或使用命令行工具.几乎每个操作系统都捆绑有这样的工具.在Mac OS X上,启动 Terminal.app 并输入 uuidgen .

Or use a command-line tool. Nearly every operating system comes bundled with such a tool. On Mac OS X, launch Terminal.app and type uuidgen.

这篇关于UUID生成的字符类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆