各类数据的序列化以及反序列化的方法(与Java服务器通讯)

来源:互联网 发布:怎么在淘宝买小电影 编辑:程序博客网 时间:2024/06/05 09:26

上次在博客中说了int类型数据的序列化以及反序列化,但是通讯的时候不止是只有int类型的数据,还有很多类型,string类型,flaot类型,bool类型等等。

显然只有int类型是不够的,所以我们要在项目中新添加一个类,专门用来序列化以反序列化数据用,这样通讯时只需要调用这个方法即可,由于数据类型比较多,我就一块写一下下,供大家参考,下面代码如下:

我先贴代码,至于注意事项我在下面说。

using System;using System.Collections.Generic;using System.Linq;using System.Text;public class ByteBuf{    private List<byte> bytes = new List<byte>();    public int Length    {        get        {            return this.bytes.Count;        }    }    public int Postion    {        get;        set;    }    public byte [] Buffer    {        get        {            return this.bytes.ToArray();        }    }    //public ByteArray()    //{    //}    /// <summary>    /// 写int类型的数据(序列化)    /// </summary>    /// <param 长度="valve"></param>    public void WriteInt(int valve)    {        byte[] by = BitConverter.GetBytes(valve);        Array.Reverse(by);        this.bytes.AddRange(by);    }    /// <summary>    /// 读int类型的数据(反序列化)    /// </summary>    /// <returns></returns>    public int ReadInt()    {        byte[] array = new byte[4];        for (int i = 0; i < 4; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 4;        Array.Reverse(array);        return BitConverter.ToInt32(array, 0);    }    /// <summary>    /// 写byte类型的数据    /// </summary>    /// <param name="value"></param>    /// <param name="offest"></param>    /// <param name="length"></param>    public void WirteBytes(byte[] value, int offest, int length)    {        for (int i = 0; i < length; i++)        {            this.bytes.Add(value[i + offest]);        }        }    /// <summary>    /// 读byte类型的数据    /// </summary>    /// <returns></returns>    public byte[] ReadBytes()    {        byte[] array = new byte[this.Length - this.Postion];        for (int i = 0; i < this.Length-this.Postion; i++)        {            array[i] = this.bytes[i + this.Postion];        }        this.Postion = this.Length;        return array;    }    /// <summary>    /// 写double类型    /// </summary>    /// <param name="dou"></param>    public void WriteDouble(double dou)    {        byte[] bs = BitConverter.GetBytes(dou);        Array.Reverse(bs);        this.bytes.AddRange(bs);    }    /// <summary>    /// 读double类型    /// </summary>    /// <returns></returns>    public double ReadDouble()    {        byte[] array = new byte[8];        for (int i = 0; i < 8; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 8;        Array.Reverse(array);        return BitConverter.ToDouble(array, 0);    }    /// <summary>    /// 写float类型    /// </summary>    /// <param name="flao"></param>    public void WriteFloat(float flao)    {        byte[] bs = BitConverter.GetBytes(flao);        Array.Reverse(bs);        this.bytes.AddRange(bs);    }    /// <summary>    /// 读float类型    /// </summary>    /// <returns></returns>    public float ReadFloat()    {        byte[] array = new byte[4];        for (int i = 0; i < 4; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 4;        Array.Reverse(array);        return BitConverter.ToSingle(array, 0);    }    /// <summary>    /// 写long类型    /// </summary>    /// <param name="lon"></param>    public void WriteLong(long lon)    {        byte[] bs = BitConverter.GetBytes(lon);        Array.Reverse(bs);        this.bytes.AddRange(bs);    }    /// <summary>    /// 读long类型    /// </summary>    /// <returns></returns>    public long ReadLong()    {        byte[] array = new byte[8];        for (int i = 0; i < 8; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 8;        Array.Reverse(array);        return BitConverter.ToInt64(array, 0);    }    /// <summary>    /// 写short类型    /// </summary>    /// <param name="sho"></param>    public void WriteShort(short sho)    {        byte[] bs = BitConverter.GetBytes(sho);        Array.Reverse(bs);        this.bytes.AddRange(bs);    }    /// <summary>    /// 读short类型    /// </summary>    /// <returns></returns>    public short ReadShort()    {        byte[] array = new byte[2];        for (int i = 0; i < 2; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 2;        Array.Reverse(array);        return BitConverter.ToInt16(array, 0);    }    /// <summary>    /// 写char类型    /// </summary>    /// <param name="cha"></param>    public void WriteChar(char cha)    {        byte[] bs = BitConverter.GetBytes(cha);        Array.Reverse(bs);        this.bytes.AddRange(bs);    }    /// <summary>    /// 读char类型    /// </summary>    /// <returns></returns>    public char ReadChar()    {        byte[] array = new byte[2];        for (int i = 0; i < 2; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 2;        Array.Reverse(array);        return BitConverter.ToChar(array, 0);    }    /// <summary>    /// 写string类型    /// </summary>    /// <param name="str"></param>    public void WriteString (string str)    {        byte[] bs = Encoding.UTF8.GetBytes(str);        Array.Reverse(bs);        this.WriteInt(bs.Length);        this.bytes.AddRange(bs);    }    /// <summary>    /// 读string类型    /// </summary>    /// <returns></returns>    public string ReadString()    {        int num = this.ReadInt();        if (num<=0)        {            return string.Empty;        }        byte[] array = new byte[num];        for (int i = 0; i < num; i++)        {            array[i] = this.bytes[i + this.Postion];        }        this.Postion += num;        Array.Reverse(array);        return Encoding.UTF8.GetString(array);    }    /// <summary>    /// 写bool类型    /// </summary>    /// <param name="value"></param>    public void WriteBoolean(bool value)    {        if (value==true)        {            byte[] bs = BitConverter.GetBytes(1);            Array.Reverse(bs);            this.bytes.AddRange(bs);        }        else        {            byte[] bs = BitConverter.GetBytes(0);            Array.Reverse(bs);            this.bytes.AddRange(bs);        }    }    /// <summary>    /// 读bool类型    /// </summary>    /// <returns></returns>    public bool ReadBoolean()    {        byte[] array = new byte[4];        for (int i = 0; i < 4; i++)        {            array[i] = this.bytes[i + this.Postion];        }        Postion += 4;        Array.Reverse(array);        int boo = BitConverter.ToInt32(array, 0);        return boo != 0;    }}


这里就是所有类型的数据的序列化以及反序列化,我在前面一篇博客说过,传输时最好在客户端进行数据反转,否则服务器收到的数据时反过来的,但是string类型的数据不用反转,任何语言都是一样的。

阅读全文
0 0