[guid] :: newguid()。getBytes()返回与[System.Text.Encoding] :: utf8.getBytes(...)不同的结果。

发布于 2025-01-18 21:30:03 字数 1209 浏览 3 评论 0原文

我在stackowerflow上的缩短指导方面找到了这种出色的方法: .NET简短唯一的识别标识符

我有一些一些我想以相同方式对待的其他字符串,但我发现在大多数情况下,base64string甚至比原始字符串更长。

我的问题是:为什么[guid] :: newGuid()。tobyTearray()返回一个比[system.text.encoding] :: utf8.getBytes([GUID)(GUID)(GUID ] :: newguid()。guid)

例如,让我们看以下GUID:

$guid = [guid]::NewGuid()
$guid

Guid
----
34c2b21e-18c3-46e7-bc76-966ae6aa06bc

使用$ GUID.Get.GetBytes(),返回以下内容:

30
178
194
52
195
24
231
70
188
118
150
106
230
170
6
188

and [System.Convert] :: tobase64String($ guid.tobytearray())生成hrlcnmmy50a8dpzq5qogva ==

[system.convert] :: tobase64string([System.text.encoding] :: utf8.getBybytes($ guid.guid)) code>>但是,返回mzrjmmiymwutmthjmy00nmu3lwjjjnzytoty2ywu2ywewewewnmjj,带有[system.text.encoding] :: utf8.getbytes($ guid.guid)($ guid.guid) at:

51
52
99
50
98
50
49
101
45
49
56
99
51
45
52
54
101
55
45
98
99
55
54
45
57
54
54
97
101
54
97
97
48
54
98
99

I found this excellent approach on shortening GUIDs here on stackowerflow: .NET Short Unique Identifier

I have some other strings that I wanted to treat the same way, but I found out that in most cases the Base64String is even longer than the original string.

My question is: why does [guid]::NewGuid().ToByteArray() return a significant smaller byte array than [System.Text.Encoding]::UTF8.GetBytes([guid]::NewGuid().Guid)?

For example, let's look at the following GUID:

$guid = [guid]::NewGuid()
$guid

Guid
----
34c2b21e-18c3-46e7-bc76-966ae6aa06bc

With $guid.GetBytes(), the following is returned:

30
178
194
52
195
24
231
70
188
118
150
106
230
170
6
188

And [System.Convert]::ToBase64String($guid.ToByteArray()) generates HrLCNMMY50a8dpZq5qoGvA==

[System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($guid.Guid)), however, returns MzRjMmIyMWUtMThjMy00NmU3LWJjNzYtOTY2YWU2YWEwNmJj, with [System.Text.Encoding]::UTF8.GetBytes($guid.Guid) being:

51
52
99
50
98
50
49
101
45
49
56
99
51
45
52
54
101
55
45
98
99
55
54
45
57
54
54
97
101
54
97
97
48
54
98
99

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

入怼 2025-01-25 21:30:03

GUID 结构是一个存储 16 字节数组的对象,包含它的价值。
这些是您在执行其方法 .ToByteArray() 方法时看到的 16 个字节。

“正常”字符串表示形式是这些字节的十六进制格式的分组系列。 (4-2-2-2-6)

至于转换为 Base64,这将始终返回更长的字符串,因为每个 Base64 数字恰好代表 6 位数据。

因此,输入的每三个 8 位字节(3×8 位 = 24 位)可以由四个 6 位 Base64 数字(4×6 = 24 位)表示。
甚至可以在字符串末尾使用 = 填充字符来扩展生成的字符串,使其始终是 4 的倍数。
结果是一个 [math]::Ceiling(/ 3) * 4 长度的字符串。

使用 [System.Text.Encoding]::UTF8.GetBytes([guid]::NewGuid().Guid) 实际上是首先执行 GUID 的 .ToString() 方法它将从该字符串返回其中每个字符的 ascii 值。
(十六进制表示 = 每个字节 2 个字符 = 32 个值 + 其中的四个破折号留下一个 36 字节数组)

The GUID struct is an object storing a 16 byte array that contains its value.
These are the 16 bytes you see when you perform its method .ToByteArray() method.

The 'normal' string representation is a grouped series of these bytes in hexadecimal format. (4-2-2-2-6)

As for converting to Base64, this will always return a longer string because each Base64 digit represents exactly 6 bits of data.

Therefore, every three 8-bits bytes of the input (3×8 bits = 24 bits) can be represented by four 6-bit Base64 digits (4×6 = 24 bits).
The resulting string can even be extended with = padding characters at the end of the string to always be a multiple of 4.
The result is a string of [math]::Ceiling(<original size> / 3) * 4 length.

Using [System.Text.Encoding]::UTF8.GetBytes([guid]::NewGuid().Guid) is actually first performing the GUID's .ToString() method and from that string it will return the ascii values of each character in there.
(hexadecimal representation = 2 characters per byte = 32 values + the four dashes in it leaves a 36-byte array)

瀞厅☆埖开 2025-01-25 21:30:03
[guid]::NewGuid().ToByteArray()

在这个问题的范围内,可以将GUID视为128位的数字(实际上是a 结构,但这与问题无关)。将其转换为字节阵列时,您将128除以8(每个字节),并获得 16字节的数组。

[System.Text.Encoding]::UTF8.GetBytes([guid]::NewGuid().Guid)

这首先将GUID转换为十六进制字符串表示。然后,该字符串被编码为UTF-8。

一个十六进制字符串使用每个输入字节(下部的一个十六进制数字,一个为4位)。因此,我们至少需要32个字符(16个字节的GUID乘以2)。当转换为UTF-8时,每个字符恰好与一个字节相关,因为所有的十六进制数字以及仪表板都在基本的ASCII范围内,该范围将1:1映射到UTF-8。因此,包括破折号我们最终以32 + 4 = 36字节

因此,这就是[System.Convert] :: TOBASE64STRING()必须在第一种情况下使用-16个字节输入,在第二种情况下为36个字节。

每个基本64输出数字最多代表6个输入位。

  • 16输入字节= 128位,除以6 = 22 base64字符

  • 36输入字节= 288位,除以6 = 48 base64字符

这是您首先将GUID转换为HEX String的Base64字符数的两倍以上。

[guid]::NewGuid().ToByteArray()

In the scope of this question, a GUID can be seen as a 128-bit number (actually it is a structure, but that's not relevant to the question). When converting it into a byte array, you divide 128 by 8 (bits per byte) and get an array of 16 bytes.

[System.Text.Encoding]::UTF8.GetBytes([guid]::NewGuid().Guid)

This converts the GUID to a hexadecimal string representation first. Then this string gets encoded as UTF-8.

A hex string uses two characters per input byte (one hex digit for the lower and one for the upper 4 bits). So we need at least 32 characters (16 bytes of GUID multiplied by 2). When converted to UTF-8 each character relates to exactly one byte, because all hex digits as well as the dash are in the basic ASCII range which maps 1:1 to UTF-8. So including the dashes we end up with 32 + 4 = 36 bytes.

So this is what [System.Convert]::ToBase64String() has to work with - 16 bytes of input in the first case and 36 bytes in the second case.

Each Base64 output digit represents up to 6 input bits.

  • 16 input bytes = 128 bits, divided by 6 = 22 Base64 characters

  • 36 input bytes = 288 bits, divided by 6 = 48 Base64 characters

That's how you end up with more than twice the number of Base64 characters when converting a GUID to hex string first.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文