英文:
Understanding the return value of MemoryMarshal.Cast
问题
我正在尝试理解新的C#类型 Span<T>
。
在阅读Microsoft的文章 All About Span: Exploring a New .NET Mainstay 时,我看到在 What Is Span<T>?
部分的底部有以下内容:
"Spans 提供了许多超出已经提到的好处。例如,spans 支持重新解释转换的概念,这意味着您可以将 Span<byte>
强制转换为 Span<int>
(其中 Span<int>
的第0个索引映射到 Span<byte>
的前四个字节)。这样,如果您读取了一个字节缓冲区,您可以将其传递给安全高效地操作分组字节作为整数的方法。"
在这篇文章中没有提供示例,但我最终在 Adam Storr的文章 中找到了这种新的方式,通过使用 MemoryMarshal.Cast
方法来实现。
然而,当我尝试这样做时,我得到了一个奇怪的结果:
var byteArray = new byte[] { 1,0,0,0, 0,1,0,0};
Span<byte> byteSpan = byteArray;
Span<int> intSpan = MemoryMarshal.Cast<byte, int>(byteSpan);
Microsoft的文章说 "Span的第0个索引映射到Span的前四个字节"。所以,通过创建一个包含8个字节的数组,我得到了一个包含2个整数的Span。
第一个整数的值是 1
,这是我预期的值(0001
),但第二个整数的值是 256
,这是我不理解的地方。
难道我不应该得到值 2
吗,因为我的字节数组的第二半部分是 0010
吗?
英文:
I'm trying to understand the new C# type Span<T>
When reading the Microsoft's article All About Span: Exploring a New .NET Mainstay I saw at the bottom of the What Is Span<T>?
section
> Spans provide a multitude of benefits beyond those already mentioned. For example, spans support the notion of reinterpret casts, meaning you can cast a Span<byte>
to be a Span<int>
(where the 0th index into the Span<int>
maps to the first four bytes of the Span<byte>
). That way if you read a buffer of bytes, you can pass it off to methods that operate on grouped bytes as ints safely and efficiently.
They don't provide any example in this article but I eventually found the (new way) of doing this in Adam Storr's article by using the MemoryMarshal.Cast
method
However when I try to do this, I get a weird result
var byteArray = new byte[] { 1,0,0,0, 0,1,0,0};
Span<byte> byteSpan = byteArray;
Span<int> intSpan = MemoryMarshal.Cast<byte, int>(byteSpan);
Microsoft's articles says the 0th index into the Span maps to the first four bytes of the Span
. So by creating an array of 8 bytes I get a Span of 2 integers.
The value of the first integer is 1
which is what I was expecting (0001
) but the for 2nd integer I get the value 256
which is what I don't understand.
Am I not supposed to get the value 2
because the 2nd half of my bytes array is 0010
?
答案1
得分: 6
1
(可以用十六进制表示为):
0x00000001
0x0001
0x01
具有大端字节模式:
0x00 0x00 0x00 0x01
和小端字节模式:
0x01 0x00 0x00 0x00
256
(可以用十六进制表示为):
0x00000100
0x0100
具有大端字节模式:
0x00 0x00 0x01 0x00
和小端字节模式:
0x00 0x01 0x00 0x00
由于您的机器是小端字节序,因此MemoryMarshal.Cast
使用小端表示数字。因此,1 0 0 0
被解释为 1
,而 0 1 0 0
被解释为 256
。
英文:
The number:
1
(which can be written in hex as):
0x00000001
0x0001
0x01
Has the big-endian byte pattern:
0x00 0x00 0x00 0x01
And the little-endian byte pattern:
0x01 0x00 0x00 0x00
The number:
256
(which can be written in hex as):
0x00000100
0x0100
Has the big-endian byte pattern:
0x00 0x00 0x01 0x00
And the little-endian byte pattern:
0x00 0x01 0x00 0x00
Since your machine is little-endian, the MemoryMarshal.Cast
is using the little-endian representations of numbers. Therefore 1 0 0 0
gets interpreted as 1
, and 0 1 0 0
gets interpreted as 256
.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论