以前在VC里我用过memset来初始化一个字节数组,不知在C#里有没有,你试试.

解决方案 »

  1.   

    在你的结构(或类)上添加标识可以序列化的属性:
    [Serializable]YourClass theData = new YourClass();
    //赋值MemoryStream stream = new MemoryStream();IFormatter formatter = new BinaryFormatter();
    formatter.Serialize(stream, theData);byte[] serializedData = stream.ToArray();stream.Close();//serializedData 就是序列化后的值,你可以通过网络传输这个值在接受的时候:byte[] serializedData = //接受数据MemoryStream stream = new MemoryStream( serializedData );
    IFormatter formatter = new BinaryFormatter();//这样通过序列化和反序列化,就可以在网络中传输对象
    YourClass theData = (YourClass)formatter.Deserialize( stream );
      

  2.   

    参考ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconbinaryserialization.htm
      

  3.   

    TO:timmy3310(tim) & dragontt(龙人) 
    先谢谢两位!
    我想我可能说的不太清楚,或者是两位给我的参考我没有看太明白。
    事实上,程序要求按照一定的格式(大小)传输信息。消息的格式已经由另一方给出。
    类似这样:
    消息一:length as uint 4 Bytes
            ID as uint 4 Bytes
            .
            .
            .
            MSGBODY_LENGTH as byte 1 Bytes 
            MSGBODY as string N bytes N=MSGBODY_LENGTH
    用formatter.Serialize(stream, theData);是否可以按上述要求得到欲传输的字节数组?
      

  4.   

    不可以,序列化只能按照.Net的格式,自定义的格式需要自己实现代码你把你的格式和结构贴出来,我帮你看看
      

  5.   

    消息结构蛮复杂的。而且太多。我没办法一个一个地贴出来。
    举几个例子好了:
    消息一:
     MSG_LENGTH uint型 4个字节 其中记录这一消息总的长度
     MSG_ID      uint型 4个字节 记录这一消息的类型
      MSG_BODY_LENGTH byte型 1个字节 其中记录MSG_BODY的长度。
      MSG_BODY  定长字符串 N(MSG_BODY_LENGTH)个字节 其中记录消息主体。
    消息一的回应:
     MSG_LENGTH uint型 4个字节 其中记录这一消息总的长度
     MSG_ID      uint型 4个字节 记录这一消息的类型
      MSG_REQ     byte型 1个字节 记录回应消息。如1表示成功。0表示失败等
    工程一开始打算用VB实现,用COPYMEMORY将内存中的结构内容拷贝到一个字节数组中
    可是在编码时COPYMEMORY经常出现不可思议的错误。加上本人想用C#做个东西。就推翻了用C#写一遍。只是还没有效率高的数据转换方法。
      

  6.   

    int msg_length,msg_id;
    byte msg_body_length;
    string msg_body;//计算值MemoryStream stream = new MemoryStream();
    StreamWriter writer = new StreamWriter(stream);writer.Write( msg_length );
    writer.Write( msg_id );
    writer.Write( msg_body_length );
    writer.Write( msg_body );writer.Flush();
    stream.Flush();byte[] theData = stream.ToArray();stream.Close();
    writer.Close();// StreamWriter的Write方法重载了很多版本,很方便使用的//你先看,马上帮你写读取的代码
      

  7.   

    是可以支持自定义序列化的
    参考
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpguide/html/cpconcustomserialization.htm
    ms-help://MS.VSCC/MS.MSDNVS.2052/cpref/html/frlrfsystemruntimeserializationserializationinfoclasstopic.htm针对你的情况
    在序列化时,使用 AddValue 方法将对象添加到 SerializationInfo;在反序列化时,使用 GetValue 方法从 SerializationInfo 中提取对象。
    只要适当的操作
    AddValue,GetValue就应该可以了吧
    (由于那个参考中提供了示例,就不多说了)
      

  8.   

    读取:
    int msg_length,msg_id;
    byte msg_req;byte[] receivedData; //这是你收到的字节数组MemoryStream stream = new MemoryStream(receivedData);byte[] buffer = null;
    stream.Read( buffer,0,4 );
    msg_length = GetIntegerFromBytes( buffer );stream.Read( buffer,4,4 );
    msg_id  = GetIntegerFromBytes( buffer );stream.Read( buffer,8,1 );
    msg_req = byffer[0];GetIntegerFromBytes是自己写的一个方法:
    private int GetIntegerFromBytes( byte[] bytes )
    {
    if( bytes.Length!=4 )
    throw new ApplicationException();
    return bytes[0]*0x1000+bytes[1]*0x100+bytes[2]*0x10+bytes[3];
    }