c#指针的使用例程

 unsafe
            {
                double value = 888888;
                byte[] v1 = BitConverter.GetBytes(value);
                byte[] v2 = new byte[v1.Length];
                double* pv = &value;
                IntPtr pValue = (IntPtr)pv;
                for(int i=0;i<v2.Length;i++)
                {
                    v2[i] = Marshal.ReadByte(pValue);
                    pValue += 1;
                }
                double value2 = BitConverter.ToDouble(v2,0);

                int[] sourceBuffer = new int[250] ;
                for(int i=0;i<sourceBuffer.Length;i++)
                {
                    sourceBuffer[i] = i;
                }
                int[] buffer = new int[10000];
                int[] buffer2 = new int[10000];
                fixed (  int* pStart =sourceBuffer)
                {
                    int error = 0;
                    IntPtr p = (IntPtr)pStart;
                    for (int i = 0; i < sourceBuffer.Length; i++)
                    {
                      
                        try
                        {
                            buffer[i] = Marshal.ReadInt32(p);
                          //  buffer2[i] = Marshal.ReadInt32((IntPtr)pStart);
                        }
                        catch (Exception e)
                        {
                            error++;
                        }
                      
                           p+=4;

                    }
                }
               
            }

猜你喜欢

转载自blog.csdn.net/u011555996/article/details/88225385