Pack_StringList.cs 3.19 KB
Newer Older
潘栩锋's avatar
潘栩锋 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace FLY.Thick.Base.OBJ_INTERFACE
{
    public class Pack_StringList 
    {
        public List<string> list;
        #region IPack 成员

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();

            byte[] bs;

            buf.AddRange(BitConverter.GetBytes(list.Count()));
            for (int i = 0; i < list.Count(); i++)
            {
                bs = Misc.Converter.StringToBytes(list[i]);
                buf.AddRange(BitConverter.GetBytes(bs.Count()));
                buf.AddRange(bs);
            }
            return buf.ToArray();
        }
        /// <summary>
        /// 返回由字节数组中指定位置的9个字节转换来的数据。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value)
        {
            int cnt = 4;
            if (value.Length < cnt)
                return false;

            int idx = 0;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;
            cnt += len * 4;
            if (value.Length < cnt)
                return false;
            if (list == null)
                list = new List<string>();
            list.Clear();
            for (int i = 0; i < len; i++)
            {
                int l = BitConverter.ToInt32(value, idx);
                idx += 4;
                cnt += l;
                if (value.Length < cnt)
                    return false;

                string s = Misc.Converter.BytesToString(value, idx, l);
                idx += l;
                list.Add(s);
            }
            return true;
        }


        #endregion
    }
    public class Pack_String
    {
        public string data;
        #region IPack 成员

        public byte[] ToBytes()
        {
            List<byte> buf = new List<byte>();
            if (data != null)
            {
                byte[] bs = Misc.Converter.StringToBytes(data);
                buf.AddRange(BitConverter.GetBytes(bs.Length));
                buf.AddRange(bs);
            }
            else
            {
                buf.AddRange(BitConverter.GetBytes(0));
            }

            return buf.ToArray();
        }
        /// <summary>
        /// 返回由字节数组中指定位置的9个字节转换来的数据。
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryParse(byte[] value, int index, int count)
        {
            int cnt = 4;
            if (value.Length < cnt)
                return false;

            int idx = 0;
            int len = BitConverter.ToInt32(value, idx);
            idx += 4;

            cnt += len;
            if (value.Length < cnt)
                return false;
            if (len <= 0)
                data = null;
            else
            {
                data = Misc.Converter.BytesToString(value, idx, len);
                idx += len;
            }
            return true;
        }

        public bool TryParse(byte[] value)
        {
            return TryParse(value, 0, value.Length);
        }
        #endregion
    }
}