C#的字节数组固定INT指针 [英] C# byte array to fixed int pointer

查看:166
本文介绍了C#的字节数组固定INT指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是它可能以某种方式投用固定的()语句



这是形势造就了指针的类型:



我有个字节,这是我想遍历数组,但我想的值被视为整型,因而具有一个int *而不是一个字节*。



下面是一些示例代码:

 字节[]与rawdata =新的字节[1024]; 

固定为(int * PTR = rawdata中)//此失败的隐式转换错误
{
的for(int i = IDX; I< rawdata.Length;我++)
{
//在这里做了一些工作
}
}

可以这样无需做迭代里面的演员做了什么?


解决方案

 字节[] = rawdata中新的字节[1024]; 

固定(字节* bptr = rawdata中)
{
为int * PTR =(INT *)bptr;
的for(int i = IDX; I< rawdata.Length;我++)
{
//在这里做了一些工作
}
}


is it possible to somehow cast the type of a pointer created by the fixed() statement?

This is the situation:

I have an array of byte, which i would like to iterate through, however i would like the values to be treated as int, thus having an int* instead of a byte*.

Here's some exemplary code:

byte[] rawdata = new byte[1024];

fixed(int* ptr = rawdata) //this fails with an implicit cast error
{
    for(int i = idx; i < rawdata.Length; i++)
    {
        //do some work here
    }
}

Can this be done without having to do the cast inside the iteration?

解决方案

byte[] rawdata = new byte[1024];

fixed(byte* bptr = rawdata)
{
    int* ptr=(int*)bptr;
    for(int i = idx; i < rawdata.Length; i++)
    {
        //do some work here
    }
}

这篇关于C#的字节数组固定INT指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆