PSI.EpicsClient2.DataPacket.GetUInt16 C# (CSharp) Метод

GetUInt16() публичный Метод

Returns an UInt16 at a given position
public GetUInt16 ( int position ) : UInt16
position int
Результат System.UInt16
        public UInt16 GetUInt16(int position)
        {
            return (UInt16)(((uint)Data[position] << 8) | Data[position + 1]);

            /*byte[] ushortBytes = new byte[2];
            Buffer.BlockCopy(Data, position, ushortBytes, 0, 2);

            Array.Reverse(ushortBytes);

            return BitConverter.ToUInt16(ushortBytes, 0);*/
        }

Usage Example

        internal object DecodeData(Type t, uint nbElements = 1, int startPost = 0, int maxSize = 40)
        {
            if (t.IsSubclassOf(typeof(Decodable)) && !t.IsArray)
            {
                Decodable res = (Decodable)t.GetConstructor(System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance, null, new Type[] { }, null).Invoke(new object[] { });
                res.Decode(this, nbElements);
                return(res);
            }

            if (t == typeof(object))
            {
                t = channelDefinedType;
            }

            Type baseT = t;

            if (baseT.IsArray)
            {
                baseT = t.GetElementType();
            }

            if (t.IsArray)
            {
                Type dl      = typeof(List <>);
                Type genList = dl.MakeGenericType(new Type[] { baseT });

                System.Collections.IList res = (System.Collections.IList)Activator.CreateInstance(genList);

                int pos         = (int)RawData.HeaderSize + startPost;
                int elementSize = TypeHandling.EpicsSize(baseT);
                for (int i = 0; i < nbElements; i++)
                {
                    switch (TypeHandling.Lookup[baseT])
                    {
                    case EpicsType.Int:
                        res.Add(RawData.GetInt32(pos));
                        break;

                    case EpicsType.Short:
                        res.Add(RawData.GetInt16(pos));
                        break;

                    case EpicsType.Float:
                        res.Add(RawData.GetFloat(pos));
                        break;

                    case EpicsType.Double:
                        res.Add(RawData.GetDouble(pos));
                        break;

                    case EpicsType.Byte:
                        res.Add(RawData.GetByte(pos));
                        break;

                    default:
                        throw new Exception("Type not supported");
                    }
                    pos += elementSize;
                }
                return(((dynamic)res).ToArray());
            }

            if (baseT == typeof(DateTime))
            {
                long     secs     = RawData.GetUInt32((int)RawData.HeaderSize + startPost);
                long     nanoSecs = RawData.GetUInt32((int)RawData.HeaderSize + startPost + 4);
                DateTime d        = (new DateTime(timestampBase.Ticks + (secs * 10000000L) + (nanoSecs / 100L))).ToLocalTime();
                return(d);
            }

            switch (TypeHandling.Lookup[baseT])
            {
            case EpicsType.Internal_UInt:
                return(RawData.GetUInt32((int)RawData.HeaderSize + startPost));

            case EpicsType.Internal_UShort:
                return(RawData.GetUInt16((int)RawData.HeaderSize + startPost));

            case EpicsType.Int:
                return(RawData.GetInt32((int)RawData.HeaderSize + startPost));

            case EpicsType.Short:
                return(RawData.GetInt16((int)RawData.HeaderSize + startPost));

            case EpicsType.Float:
                return(RawData.GetFloat((int)RawData.HeaderSize + startPost));

            case EpicsType.Double:
                return(RawData.GetDouble((int)RawData.HeaderSize + startPost));

            case EpicsType.String:
                return(RawData.GetDataAsString(startPost, maxSize));

            case EpicsType.Byte:
                return(RawData.GetByte((int)RawData.HeaderSize + startPost));

            default:
                //throw new Exception("Type not supported");
                return(new object());
            }
        }