为什么 Guid.ToString(“n”) 与从相同 guid 的字节数组生成的十六进制字符串不同?

发布于 2024-09-10 19:35:09 字数 707 浏览 3 评论 0原文

考虑以下单元测试:

    [TestMethod]
    public void TestByteToString()
    {
        var guid = new Guid("61772f3ae5de5f4a8577eb1003c5c054");
        var guidString = guid.ToString("n");
        var byteString = ToHexString(guid.ToByteArray());

        Assert.AreEqual(guidString, byteString);
    }

    private String ToHexString(Byte[] bytes)
    {
        var hex = new StringBuilder(bytes.Length * 2);
        foreach(var b in bytes)
        {
            hex.AppendFormat("{0:x2}", b);
        }
        return hex.ToString();
    }

结果如下:

Assert.AreEqual 失败。预期:<61772f3ae5de5f4a8577eb1003c5c054>。实际:<3a2f7761dee54a5f8577eb1003c5c054>。

Consider the following unit test:

    [TestMethod]
    public void TestByteToString()
    {
        var guid = new Guid("61772f3ae5de5f4a8577eb1003c5c054");
        var guidString = guid.ToString("n");
        var byteString = ToHexString(guid.ToByteArray());

        Assert.AreEqual(guidString, byteString);
    }

    private String ToHexString(Byte[] bytes)
    {
        var hex = new StringBuilder(bytes.Length * 2);
        foreach(var b in bytes)
        {
            hex.AppendFormat("{0:x2}", b);
        }
        return hex.ToString();
    }

Here's the result:

Assert.AreEqual failed. Expected:<61772f3ae5de5f4a8577eb1003c5c054>. Actual:<3a2f7761dee54a5f8577eb1003c5c054>.

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

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

发布评论

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

评论(3

抚你发端 2024-09-17 19:35:09

好吧,在前 4 个字节之后,它们是相同的。前四个是一样的,只是顺序相反。

基本上,当从字符串创建时,假定它采用“big-endian”格式:最高字节在左边。但是,当在内部存储时(在 Intel-ish 机器上),字节按“little-endian”排序:最高位字节在右侧。

Well, they are the same, after the first 4 bytes. And the first four are the same, just in the reverse order.

Basically, when created from the string, it's assumed to be in "big-endian" format: Highest byte to the left. However, when stored internally (on an Intel-ish machine), the bytes are ordered "little-endian": highest order byte to the right.

谷夏 2024-09-17 19:35:09

如果比较结果,您可以看到前三组是相反的:

61 77 2f 3a   e5 de   5f 4a   8577eb1003c5c054
3a 2f 77 61   de e5   4a 5f   8577eb1003c5c054

那是因为在 GUID 结构,这 3 组被定义为 DWORD 和两个 WORD 而不是字节:

{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

因此,在内存中,英特尔处理器以 Little-endian 顺序存储它们(最高有效字节在最后)。

If you compare the results, you can see that the first three groups are reversed:

61 77 2f 3a   e5 de   5f 4a   8577eb1003c5c054
3a 2f 77 61   de e5   4a 5f   8577eb1003c5c054

That's because in the GUID structure, these 3 groups are defined as DWORD and two WORDs rather than bytes:

{0x00000000,0x0000,0x0000,{0x00,0x00,0x00,0x00,0x00,0x00,0x00,0x00}}

so in memory, an Intel processor stores them in Little-endian order (the most significant byte the last).

随遇而安 2024-09-17 19:35:09

GUID 的结构如下:

int a
short b
short c
byte[8] d

因此,对于 a 表示的部分,您的代码会反转字节。所有其他部分均已正确转换。

A GUID is structured as follows:

int a
short b
short c
byte[8] d

So for the part represented by a your code gets the bytes reversed. All other parts are transformed correctly.

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