为什么Guid.ToByteArray()命令字节它的方式? [英] Why does Guid.ToByteArray() order the bytes the way it does?

查看:154
本文介绍了为什么Guid.ToByteArray()命令字节它的方式?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当你调用的toByteArray()上一个GUID在.NET中,所产生的数组中的字节顺序是不是你所期望相比,串再GUID的presentation。例如,对于psented作为字符串以下GUID重新$ P $

When you call ToByteArray() on a GUID in .NET, the ordering of the bytes in the resulting array is not what you'd expect as compared to the string representation of the GUID. For example, for the following GUID represented as a string:

11223344-5566-7788-9900-aabbccddeeff

的toByteArray()的结果是这样的:

44, 33, 22, 11, 66, 55, 88, 77, 99, 00, AA, BB, CC, DD, EE, FF

请注意,第一四字节的顺序是相反的。还字节4和5被交换和字节6和7被交换。但最终的8个字节是在同一顺序他们重新psented作为字符串$ p中$

Note that the order of the first four bytes is reversed. Also bytes 4 and 5 are swapped and bytes 6 and 7 are swapped. But the final 8 bytes are in the same order they're represented as in the string.

据我所知,这是发生。我想知道的是为什么.NET处理这种方式。

I understand that this is occurring. What I would like to know is why .NET handles it this way.

有关的参考,你可以看到一些讨论和混乱这个(不正确归因于Oracle数据库)<一href="http://stackoverflow.com/questions/2667740/convert-byte-array-from-oracle-raw-to-system-guid">here和<一href="http://stackoverflow.com/questions/7289734/convert-from-oracles-raw16-to-nets-guid">here.

For reference, you can see some discussion and confusion about this (incorrect attributed to Oracle databases) here and here.

推荐答案

如果你读从示例部分该GUID构造,你会发现你的回答:

If you read the Examples section from the GUID constructor, you'll find your answer:

的Guid(1,2,3,新的byte [] {0,1,2,3,4,5,6,7})创建一个GUID,对应于00000001-0002-0003-0001-020304050607

Guid(1,2,3,new byte[]{0,1,2,3,4,5,6,7}) creates a Guid that corresponds to "00000001-0002-0003-0001-020304050607".

A 是一个32位整数, B 是一个16位整数, C 是一个16位整数,而 D 简直就是8个字节。

a is a 32-bit integer, b is a 16-bit integer, c is a 16-bit integer, and d is simply 8 bytes.

由于 A B C 是整数类型,而不是原始字节,他们是受端选择如何显示它们订货时。该 RFC的GUID的(RFC4122)规定,他们应该psented用big endian格式$ P $

Because a, b, and c are integer types rather than raw bytes, they are subject to endian ordering when choosing how to display them. The RFC for GUID's (RFC4122) states that they should be presented in big endian format.

这篇关于为什么Guid.ToByteArray()命令字节它的方式?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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