BitConverter提供了很多方法:
转换到字节数组的:
public static byte[] GetBytes(bool);
public static byte[] GetBytes(char);
public static byte[] GetBytes(double);
public static byte[] GetBytes(short);
public static byte[] GetBytes(int);
public static byte[] GetBytes(long);
public static byte[] GetBytes(float);
public static byte[] GetBytes(ushort);
public static byte[] GetBytes(uint);
public static byte[] GetBytes(ulong);

解决方案 »

  1.   

    字节数组到别的类型:
    ToBoolean(byte[] value,int startIndex); 
    ToChar(byte[] value,int startIndex);  
    ToDouble(byte[] value,int startIndex);  
    ToInt16(byte[] value,int startIndex);  
    ToInt32(byte[] value,int startIndex);  
    ToInt64(byte[] value,int startIndex);  
    ToSingle(byte[] value,int startIndex);  
    ToString(byte[] value);  
    ToString(byte[] value,int startIndex);  
    ToString(byte[] value,int startIndex,int length);  
    ToUInt16(byte[] value,int startIndex);  
    ToUInt32(byte[] value,int startIndex);  
    ToUInt64(byte[] value,int startIndex);  如果是结构的话,序列化是最直接的方式,如果不能序列化,那只能自己定义转换的方式了,.net没有提供直接转换的方式。可以根据基本类型的转换来转换结构。
      

  2.   

    请看,我是想把结构体struct转换成byte[],比如是:
    public struct float1
    {
    float a;
    float b;
    public float1(float c,float d)
    {
    a=c;
    b=d;
    }
    }
    float1 testfloat= new float1(20.1f,23.2f);这个结构可以直接转换么?如果不行,如何用其他的方法转换?
      

  3.   

    byte[] abytes = BitConverter.GetBytes(testfloat.a);
    byte[] bbytes = BitConverter.GetBytes(testfloat.b);
    byte[] bytes = new byte[abytes.Length + bbytes.Length];
    Buffer.BlockCopy(abytes, 0, bytes, 0, abytes.Length);
    Buffer.BlockCopy(bbytes, 0, bytes, abytes.Length, bbytes.Length);
    bytes就是转换后的结果。
      

  4.   

    我无比感谢,能继续帮助嘛?在oicq上联系,或者email.我现在的分值很少,估计问不了几个问题。呵呵。我的email:[email protected],oicq:40955189(id:江边皓)
      

  5.   

    to xixigongzhu(夕夕公主) 
    上班时间不能上oicq,但是希望能和你email联系,没有分给的朋友你要不要呀?
      

  6.   

    BinaryFormatter bf = new BinaryFormatter();
    MemoryStream ms = new MemoryStream();
    InfoStruct info = new InfoStruct();
    info.flag = "login";
    info.parameter1 = account;
    info.parameter2 = pwd;
    bf.Serialize(ms, info);
    byte[] byteData = ms.ToArray();