Пример #1
0
        /// <summary>
        /// Convert bit array to object based on position, type of object and length of the bits
        /// </summary>
        /// <param name="bit_array">the input bit array</param>
        /// <param name="cursor">the start position</param>
        /// <param name="obj">output value</param>
        /// <param name="type">conversion type</param>
        /// <param name="field_len">field length, if it's 0, the field length will be determined by type</param>
        public static void ConvertBitArrayToObj(ref BitArray bit_array, ref int cursor, out Object obj, Type type, int field_len)
        {
            if (type.Equals(typeof(bool)))
            {
                obj = bit_array[cursor];
                cursor++;
            }
            else if (type.Equals(typeof(byte)))
            {
                obj = (byte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8);
            }
            else if (type.Equals(typeof(sbyte)))
            {
                obj = (sbyte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8);
            }
            else if (type.Equals(typeof(UInt16)))
            {
                obj = (UInt16)(UInt64)CalculateVal(ref bit_array, ref cursor, 16);
            }
            else if (type.Equals(typeof(Int16)))
            {
                obj = (Int16)(UInt64)CalculateVal(ref bit_array, ref cursor, 16);
            }
            else if (type.Equals(typeof(UInt32)))
            {
                obj = (UInt32)(UInt64)CalculateVal(ref bit_array, ref cursor, field_len);
            }
            else if (type.Equals(typeof(Int32)))
            {
                obj = (Int32)(UInt64)CalculateVal(ref bit_array, ref cursor, 32);
            }
            else if (type.Equals(typeof(UInt64)))
            {
                obj = (UInt64)(UInt64)CalculateVal(ref bit_array, ref cursor, 64);
            }
            else if (type.Equals(typeof(Int64)))
            {
                obj = (Int64)(UInt64)CalculateVal(ref bit_array, ref cursor, 64);
            }
            else if (type.Equals(typeof(string)))
            {
                string s = string.Empty;
                for (int i = 0; i < field_len; i++)
                {
                    try
                    {
                        byte bd = (byte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8);
                        System.Text.UTF8Encoding encoding = new UTF8Encoding();
                        byte[] bdarr = new byte[1] {
                            bd
                        };
                        s += encoding.GetString(bdarr);
                    }
                    catch
                    {
                    }
                }

                if (field_len > 1 && s[field_len - 1] == 0)
                {
                    s = s.Substring(0, field_len - 1);
                }
                obj = s;
            }
            else if (type.Equals(typeof(ByteArray)))
            {
                obj = new ByteArray();
                for (int i = 0; i < field_len; i++)
                {
                    ((ByteArray)obj).Add((byte)(UInt64)CalculateVal(ref bit_array, ref cursor, 8));
                }
            }
            else if (type.Equals(typeof(UInt16Array)))
            {
                obj = new UInt16Array();
                for (int i = 0; i < field_len; i++)
                {
                    ((UInt16Array)obj).Add((UInt16)(UInt64)CalculateVal(ref bit_array, ref cursor, 16));
                }
            }
            else if (type.Equals(typeof(UInt32Array)))
            {
                obj = new UInt32Array();
                for (int i = 0; i < field_len; i++)
                {
                    ((UInt32Array)obj).Add((UInt32)(UInt64)CalculateVal(ref bit_array, ref cursor, 32));
                }
            }
            else if (type.Equals(typeof(TwoBits)))
            {
                obj     = new TwoBits(bit_array[cursor], bit_array[cursor + 1]);
                cursor += 2;
            }
            else if (type.Equals(typeof(BitArray)))
            {
                obj = new BitArray(field_len);

                for (int i = 0; i < field_len; i++)
                {
                    ((BitArray)obj)[i] = bit_array[cursor];
                    cursor++;
                }
            }
            else if (type.Equals(typeof(LLRPBitArray)))
            {
                obj = new LLRPBitArray();

                int mod = field_len % 8;

                int total_len = (mod > 0) ? (field_len + 8 - mod) : field_len;

                for (int i = 0; i < total_len; i++)
                {
                    if (i < field_len)
                    {
                        ((LLRPBitArray)obj).Add(bit_array[cursor]);
                    }
                    cursor++;
                }
            }
            else
            {
                obj = (UInt32)(UInt64)CalculateVal(ref bit_array, ref cursor, field_len);
            }
        }
Пример #2
0
        /// <summary>
        /// Parse array type supported by LLRP protocol
        /// </summary>
        /// <param name="val">input string value</param>
        /// <param name="type">array type (string), defined in LLRP protocol</param>
        /// <param name="format">format (string), defined in LLRP protocol</param>
        /// <returns></returns>
        public static object ParseArrayTypeFromString(string val, string type, string format)
        {
            try
            {
                switch (type)
                {
                case "u1v":
                case "u96":
                    switch (format)
                    {
                    case "Hex":
                        return(LLRPBitArray.FromHexString(val));

                    case "Dec":
                    default:
                        return(LLRPBitArray.FromString(val));
                    }
                    break;

                case "bytesToEnd":
                case "u8v":
                    switch (format)
                    {
                    case "Hex":
                        return(ByteArray.FromHexString(val));

                    case "Dec":
                    default:
                        return(ByteArray.FromString(val));
                    }
                    break;

                case "u16v":
                    switch (format)
                    {
                    case "Hex":
                        return(UInt16Array.FromHexString(val));

                    case "Dec":
                    default:
                        return(UInt16Array.FromString(val));
                    }
                    break;

                case "u32v":
                    switch (format)
                    {
                    case "Hex":
                        return(UInt32Array.FromHexString(val));

                    case "Dec":
                    default:
                        return(UInt32Array.FromString(val));
                    }
                    break;

                case "utf8v":
                    return(val);

                    break;

                default:
                    throw new Exception(string.Format("{0} is unsupported type.", type));
                }
            }
            catch
            {
                throw new Exception(string.Format("Can't parse {0} to {1} as format {2}", val, type, format));
            }
        }