sbyte[] 的 AsBuffer()?
AsBuffer() for sbyte[]?
我正在做一个小任务,需要我从字节数组创建 IBuffer 实例。
对于常规字节数组,System.Runtime.InteropServices.WindowsRuntime 中的 .AsByte()
扩展方法工作得很好,但是我的许多数组实际上是 sbyte[]
(代码是旧版 Java代码,其中一些值被定义为例如 new sbyte[] { -86, 27, 28, 29, -1 };
是否有涵盖 sbyte[]
用例的扩展方法?遗憾的是,关于 sbyte 数组操作(以及转换为字节数组)的信息不多。
只需将 sbyte[]
转换为 byte[]
,然后使用其他方法。举个例子。
sbyte[] signed = { -2, -1, 0, 1, 2 };
byte[] unsigned = (byte[]) (Array)signed;
在AsBuffer()
之前可以试试这个转换方法
var signedBytes = new sbyte[] { -86, 27, 28, 29, -1 };
IBuffer buffer = Array.ConvertAll(signedBytes, b => (byte)b).AsBuffer();
Array.ConvertAll Documentation
该方法是.NET 2.0引入的,之后所有版本都存在
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
编辑:
进一步讨论后,似乎此代码位于 Class Library (Portable)
而非常规 Class Library
中,因此 ConvertAll
不可用。
用这个版本可以实现上面的。
var signedBytes = new sbyte[] { -86, 27, 28, 29, -1 };
IBuffer buffer = signedBytes.Cast<byte>().ToArray().AsBuffer();
我正在做一个小任务,需要我从字节数组创建 IBuffer 实例。
对于常规字节数组,System.Runtime.InteropServices.WindowsRuntime 中的 .AsByte()
扩展方法工作得很好,但是我的许多数组实际上是 sbyte[]
(代码是旧版 Java代码,其中一些值被定义为例如 new sbyte[] { -86, 27, 28, 29, -1 };
是否有涵盖 sbyte[]
用例的扩展方法?遗憾的是,关于 sbyte 数组操作(以及转换为字节数组)的信息不多。
只需将 sbyte[]
转换为 byte[]
,然后使用其他方法。举个例子。
sbyte[] signed = { -2, -1, 0, 1, 2 };
byte[] unsigned = (byte[]) (Array)signed;
在AsBuffer()
var signedBytes = new sbyte[] { -86, 27, 28, 29, -1 };
IBuffer buffer = Array.ConvertAll(signedBytes, b => (byte)b).AsBuffer();
Array.ConvertAll Documentation
该方法是.NET 2.0引入的,之后所有版本都存在
Namespace: System
Assembly: mscorlib (in mscorlib.dll)
编辑:
进一步讨论后,似乎此代码位于 Class Library (Portable)
而非常规 Class Library
中,因此 ConvertAll
不可用。
用这个版本可以实现上面的。
var signedBytes = new sbyte[] { -86, 27, 28, 29, -1 };
IBuffer buffer = signedBytes.Cast<byte>().ToArray().AsBuffer();