大家好:我用filestream里的read方法把一个文件读进了一个字节数组 。请问,我怎么才能把它读进一个结构体里,比如头两个字节是赋值给结构体的第一个成员,后两个给第二个成员,一直往下,
是需要序列化吗,谢谢大家!!

解决方案 »

  1.   

    不需要序列化,相关可以参考SDK的文档,有关stringbuffer的方法
      

  2.   

    用Marshal.Copy 将字节复制到一个非托管内存指针
    再Marshal.PtrToStructure 将数据从非托管内存块封送到托管对象
      

  3.   

    试一下,这个可不可以.
    public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        public class StructSAM
            {
                public string First;
                public string Second;
                public string Third;
                public string Nth;
                public void sa()
                { }
            }        public void doThing()
            {
                byte[] byteTemp;
                using (FileStream fs = new FileStream(@"C:\temp.jpg", FileMode.Open))
                {
                    byteTemp = new byte[fs.Length];
                    fs.Read(byteTemp, 0, byteTemp.Length);
                    fs.Close();
                }            Type sam = typeof(StructSAM);
                FieldInfo[] mInfo = sam.GetFields();
                //MemberInfo[]
                StructSAM s = new StructSAM();            int sourceIndex = 0;
                for( int i = 0; i<mInfo.Length; i++ )
                {
                    //MessageBox.Show(mInfo[i].Name);
                    byte[] bytTemp = new byte[5];
                    Array.Copy(byteTemp, sourceIndex, bytTemp, 0, 2);
                    mInfo[i].SetValue(s, System.Text.Encoding.Default.GetString(bytTemp));
                    sourceIndex += 2;
                }
                for (int i = 0; i < mInfo.Length; i++)
                {
                    string strTemp = mInfo[i].GetValue(s).ToString();
                    MessageBox.Show(strTemp);
                }
            }        private void button1_Click(object sender, EventArgs e)
            {
                doThing();
            }
        }
      

  4.   

    对了,有三个引用:using System.Text;
    using System.IO;
    using System.Reflection;
      

  5.   

    使用CopyMemory就可以达到你的目的,如下是最简单的应用
    public class MainForm : System.Windows.Forms.Form
    {
    [DllImport("Kernel32.dll",EntryPoint="RtlMoveMemory")] 
    public static extern void CopyMemory (ref Test Source,IntPtr Destination,int Length); unsafe private void button1_Click(object sender, System.EventArgs e)
    {
    Test test = new Test();
    char cTemp = 'A';  char * c = &cTemp;

    IntPtr i = (IntPtr)c; CopyMemory(ref test, i, 2);
    }
    }[StructLayout(LayoutKind.Sequential)]
    public struct Test
    {
    public char head;
    }ps:如果你需要得到如数组等引用类型的指针,请使用fiexd关键字。
      

  6.   

    如下是string类型的应用,在某些情况下需要注意编码格式所以需要转换。public class MainForm : System.Windows.Forms.Form
    {
    [DllImport("Kernel32.dll",EntryPoint="RtlMoveMemory")] 
    public static extern void CopyMemory (ref Test Source,IntPtr Destination,int Length); unsafe private void button1_Click(object sender, System.EventArgs e)
    {
    Test test = new Test();
    byte[] bytes = System.Text.Encoding.Default.GetBytes("ABCDEFG"); fixed(byte* b = bytes)
    {
    IntPtr i = (IntPtr)b; CopyMemory(ref test, i, bytes.Length);
    }
    }
    }[StructLayout(LayoutKind.Sequential)]
    public struct Test
    {
    public char head1;
    public char head2;
    public char head3;
    public char head4;
    public char head5;
    public char head6;
    public char head7;
    }
      

  7.   

    不同成员只要是char,int这些限定长度的数据类型是没有关系的。CopyMemory就是将某个地址的数据俺一定长度Copy到另一个地址。
    ps:最近问这个问题的人好多,楼主的QQ是"会飞的鱼丸"?