加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

C#不安全值类型数组到字节数组转换

发布时间:2020-12-15 06:49:24 所属栏目:百科 来源:网络整理
导读:我使用扩展方法将float数组转换为字节数组: public static unsafe byte[] ToByteArray(this float[] floatArray,int count){ int arrayLength = floatArray.Length count ? count : floatArray.Length; byte[] byteArray = new byte[4 * arrayLength]; fixe
我使用扩展方法将float数组转换为字节数组:
public static unsafe byte[] ToByteArray(this float[] floatArray,int count)
{
    int arrayLength = floatArray.Length > count ? count : floatArray.Length;
    byte[] byteArray = new byte[4 * arrayLength];
    fixed (float* floatPointer = floatArray)
    {
        fixed (byte* bytePointer = byteArray)
        {
            float* read = floatPointer;
            float* write = (float*)bytePointer;
            for (int i = 0; i < arrayLength; i++)
            {
                *write++ = *read++;
            }
        }
    }
    return byteArray;
}

我知道一个数组是一个指针,指向与内容相关的信息类型和数量的元素.此外,在我看来,没有办法从一个字节数组进行转换,而不复制上述数据.

我明白了吗编写IL来创建一个数组从指针,类型和长度甚至不可能不复制数据?

编辑:感谢您的答案,我学到了一些基础知识,并尝试新的技巧!

最初接受Davy Landman的回答后,我发现,虽然他的辉煌的StructLayout黑客将字节数组转换成浮点数组,但是它并没有相反的工作.展示:

[StructLayout(LayoutKind.Explicit)]
struct UnionArray
{
    [FieldOffset(0)]
    public Byte[] Bytes;

    [FieldOffset(0)]
    public float[] Floats;
}

static void Main(string[] args)
{
    // From bytes to floats - works
    byte[] bytes = { 0,1,2,4,8,16,32,64 };
    UnionArray arry = new UnionArray { Bytes = bytes };
    for (int i = 0; i < arry.Bytes.Length / 4; i++)
        Console.WriteLine(arry.Floats[i]);

    // From floats to bytes - index out of range
    float[] floats = { 0.1f,0.2f,0.3f };
    arry = new UnionArray { Floats = floats };
    for (int i = 0; i < arry.Floats.Length * 4; i++)
        Console.WriteLine(arry.Bytes[i]);
}

看来CLR看到两个数组的长度相同.如果结构体是由float数据创建的,则字节数组的长度太短.

解决方法

是的,类型信息和数据在相同的内存块中,所以这是不可能的,除非你覆盖一个float数组中的类型信息来愚弄系统的字节数组.这将是一个非常丑陋的黑客,可以很容易地炸毁…

如果您喜欢,可以转换浮动数据而不使用不安全的代码:

public static byte[] ToByteArray(this float[] floatArray) {
    int len = floatArray.Length * 4;
    byte[] byteArray = new byte[len];
    int pos = 0;
    foreach (float f in floatArray) {
        byte[] data = BitConverter.GetBytes(f);
        Array.Copy(data,byteArray,pos,4);
        pos += 4;
    }
    return byteArray;
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读