Пример #1
0
        /// <summary>
        /// Convert a binary string to LLRPBitArray
        /// </summary>
        /// <param name="str">Binary string, for example: "1110010001"</param>
        /// <returns></returns>
        public static LLRPBitArray FromBinString(string str)
        {
            LLRPBitArray myBitArray = new LLRPBitArray();

            for (int i = 0; i < str.Length; i++)
            {
                myBitArray.Add((str[i] == '1') ? true : false);
            }

            return(myBitArray);
        }
Пример #2
0
        /// <summary>
        /// Convert a Hex string to LLRPBitArray
        /// </summary>
        /// <param name="str">Hex string, for example: "EEFF"</param>
        /// <returns></returns>
        public static LLRPBitArray FromHexString(string str)
        {
            string s_temp = Util.ConvertHexStringToBinaryString(str);

            return(LLRPBitArray.FromBinString(s_temp));
        }
Пример #3
0
 /// <summary>
 /// Convert a Hex string to LLRPBitArray
 /// </summary>
 /// <param name="str">Hex string, for example: "EEFF"</param>
 /// <returns></returns>
 public static LLRPBitArray FromString(string str)
 {
     return(LLRPBitArray.FromHexString(str));
 }
Пример #4
0
        public static object ParseArrayTypeFromString(string rawval, string type, string format)
        {
            string str = rawval.Trim();

            try
            {
                switch (type)
                {
                case "u1v":
                case "u96":
                    switch (format)
                    {
                    case "Hex":
                        return((object)LLRPBitArray.FromHexString(str));

                    default:
                        return((object)LLRPBitArray.FromString(str));
                    }

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

                    default:
                        return((object)ByteArray.FromString(str));
                    }

                case "s8v":
                    switch (format)
                    {
                    case "Hex":
                        return((object)SignedByteArray.FromHexString(str));

                    default:
                        return((object)SignedByteArray.FromString(str));
                    }

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

                    default:
                        return((object)UInt16Array.FromString(str));
                    }

                case "s16v":
                    switch (format)
                    {
                    case "Hex":
                        return((object)Int16Array.FromHexString(str));

                    default:
                        return((object)Int16Array.FromString(str));
                    }

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

                    default:
                        return((object)UInt32Array.FromString(str));
                    }

                case "s32v":
                    switch (format)
                    {
                    case "Hex":
                        return((object)Int32Array.FromHexString(str));

                    default:
                        return((object)Int32Array.FromString(str));
                    }

                case "utf8v":
                    return((object)str);

                default:
                    throw new Exception(string.Format("{0} is unsupported type.", (object)type));
                }
            }
            catch
            {
                throw new Exception(string.Format("Can't parse {0} to {1} as format {2}", (object)str, (object)type, (object)format));
            }
        }
Пример #5
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);
            }
        }
Пример #6
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));
            }
        }