如何转换列表C# 中的 Byte[]

发布于 2024-10-07 04:22:55 字数 328 浏览 8 评论 0原文

从 Double[] src 到 Byte[] dst 的转换 可以在 C# 中通过固定指针有效地完成:

fixed( Double* pSrc = src)
{
  fixed( Byte* pDst = dst)
  {
    Byte* ps = (Byte*)pSrc;
    for (int i=0; i < dstLength; i++)
    {
      *(pDst + i) = *(ps +i);
    }
  }
}

如何对 List src 执行相同操作? 即如何获得指向数组 Double[] 的固定指针 包含在列表中? 谢谢。

Convertion from Double[] src to Byte[] dst
can be efficiently done in C# by fixed pointers:

fixed( Double* pSrc = src)
{
  fixed( Byte* pDst = dst)
  {
    Byte* ps = (Byte*)pSrc;
    for (int i=0; i < dstLength; i++)
    {
      *(pDst + i) = *(ps +i);
    }
  }
}

How can I do the same for List src ?
I.e. how can I get fixed pointer to array Double[]
included in List ?
Thanks.

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

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

发布评论

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

评论(7

Bonjour°[大白 2024-10-14 04:22:55

我之前使用过这些辅助方法:

byte[] GetBytesBlock(double[] values)
{
   var result = new byte[values.Length * sizeof(double)];
   Buffer.BlockCopy(values, 0, result, 0, result.Length);
   return result;
}

double[] GetDoublesBlock(byte[] bytes)
{
   var result = new double[bytes.Length / sizeof(double)];
   Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
   return result;
}

一个例子:

List<double> myList = new List<double>(){ 1.0, 2.0, 3.0};

//to byte[]
var byteResult = GetBytesBlock(myList.ToArray());

//back to List<double>
var doubleResult = GetDoublesBlock(byteResult).ToList();

I have used these helper methods before:

byte[] GetBytesBlock(double[] values)
{
   var result = new byte[values.Length * sizeof(double)];
   Buffer.BlockCopy(values, 0, result, 0, result.Length);
   return result;
}

double[] GetDoublesBlock(byte[] bytes)
{
   var result = new double[bytes.Length / sizeof(double)];
   Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length);
   return result;
}

An example:

List<double> myList = new List<double>(){ 1.0, 2.0, 3.0};

//to byte[]
var byteResult = GetBytesBlock(myList.ToArray());

//back to List<double>
var doubleResult = GetDoublesBlock(byteResult).ToList();
傾城如夢未必闌珊 2024-10-14 04:22:55

不确定你的意图是什么,但我认为......你想要
System.Runtime.Interopservices.Marshal.StructToPtr

not sure what you are intending, but I think ... you want
System.Runtime.Interopservices.Marshal.StructToPtr.

秋心╮凉 2024-10-14 04:22:55

您始终可以在 List 对象上使用 ToArray() 方法来获取 Double[]

You can always use the ToArray() method on the List<Double> object to get a Double[].

七婞 2024-10-14 04:22:55

您可以使用反射来获取对 List 实例中的私有 T[] _items 字段的引用。

警告:在代码片段中,您需要确保 dstLength 是 dst 和 src 长度(以字节为单位)中的最小值,这样您就不会尝试复制比可用字节更多的字节。可能您是通过创建 dst 来实现的,其大小与 src 完全匹配,但您的代码片段并没有说明这一点。

You can use reflection to get the reference to the private T[] _items field, in the List instance.

Warning: In your code snippet, you need to make sure dstLength is the minimum of dst and src lengths in bytes, so that you don't try to copy more bytes than what are available. Probably you do so by creating dst with exactly the needed size to match the src, but your snippet doesn't make it clear.

半寸时光 2024-10-14 04:22:55

使用 List.ToArray() 方法并对结果数组进行操作。

Use the List<T>.ToArray() method and operate on the resulting array.

终难遇 2024-10-14 04:22:55

这可能有效,但您将丢失数据 - 数组的内容将为 3 和 34 。

    List<double> list = new List<double>();
    list.Add(Math.PI);
    list.Add(34.22);

    byte[] arr = (from l in list
                  select (byte)l).ToArray<byte>();

This might work, but you will have a data loss- content of the array will be 3 and 34 .

    List<double> list = new List<double>();
    list.Add(Math.PI);
    list.Add(34.22);

    byte[] arr = (from l in list
                  select (byte)l).ToArray<byte>();
如痴如狂 2024-10-14 04:22:55

为什么不像往常一样访问该列表呢?

List<double> list = new List<double>();
list.Add(Math.PI);
list.Add(34.22);

byte[] res = new byte[list.Count * sizeof(double)];

unsafe
{
    fixed (byte* pres = res)
    {
        for (int i = 0; i < list.Count; i++)
        {
            *(((double*)pres) + i) = list[i];
        }
    }
}

我还没有彻底测试它,我很少需要不安全的代码,但它似乎工作得很好。

编辑:这是另一个(我个人更推荐)解决方案,没有不安全的代码:

int offset = 0;
for (int i = 0; i < list.Count; i++)
{
    long num = BitConverter.DoubleToInt64Bits(list[i]);

    for (int j = 0; j < 8; j++)
    {
        res[offset++] = (byte)num;
        num >>= 8;
    }
}

Why don't you just access the list as usual?

List<double> list = new List<double>();
list.Add(Math.PI);
list.Add(34.22);

byte[] res = new byte[list.Count * sizeof(double)];

unsafe
{
    fixed (byte* pres = res)
    {
        for (int i = 0; i < list.Count; i++)
        {
            *(((double*)pres) + i) = list[i];
        }
    }
}

I haven't tested it thoroughly and i seldomly need unsafe code, but it seems to work fine.

Edit: here is another (imo preferable) solution, without unsafe code:

int offset = 0;
for (int i = 0; i < list.Count; i++)
{
    long num = BitConverter.DoubleToInt64Bits(list[i]);

    for (int j = 0; j < 8; j++)
    {
        res[offset++] = (byte)num;
        num >>= 8;
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文