示例#1
0
        protected byte[] GetBcdBytes(ulong value, int length, int minLength = 0, int maxLength = -1, bool minimizeLength = false)
        {
            // First we want the size to be the max of the length passed in and minLength
            // Then we want it to be the min of maxLength (if it's not -1) and length
            // At the end if minimizeLength is true and we haven't filled the full array
            // we just want to return the bytes we did use
            int arrayLength = Math.Max(length, minLength);

            if (maxLength != -1)
            {
                arrayLength = Math.Min(arrayLength, maxLength);
            }

            byte[] byteArray = new byte[arrayLength];

            int index = arrayLength - 1;

            while (value > 0 && index >= 0)
            {
                byte currentValue = (byte)(value % 10);
                value        /= 10;
                currentValue += (byte)((value % 10) << 4);
                value        /= 10;

                byteArray[index--] = currentValue;
            }

            if (minimizeLength && arrayLength > 0 && index >= 0)
            {
                int bytesToTake = Math.Max(minLength, (arrayLength - index) - 1);
                byteArray = ArrayOps.GetSubArray(byteArray, arrayLength - bytesToTake, bytesToTake);
            }

            return(byteArray);
        }
示例#2
0
        public override DateTime Deserialize(byte[] bytes, ref int currentArrayIndex, int length, ref DeserializeStatus status)
        {
            // TODO: Should the length parameter be checked against GetOutputLength?
            int      outputLength = GetOutputLength();
            DateTime returnValue  = DateTime.ParseExact(ArrayOps.GetHexStringFromArray(bytes, currentArrayIndex, outputLength), _propertyInfo.MessagePropertyAttribute.Format, null);

            currentArrayIndex += outputLength;
            return(returnValue);
        }
示例#3
0
        public override byte[] Calculate(params byte[][] arrays)
        {
            byte[] combinedArray = ArrayOps.Combine(arrays);

            using (THashAlgorithm hashAlgorithm = CreateHashAlgorithm())
            {
                byte[] bytes = hashAlgorithm.ComputeHash(combinedArray);
                return(bytes);
            }
        }
示例#4
0
        public override TEnumType Deserialize(byte[] bytes, ref int currentArrayIndex, int length, ref DeserializeStatus status)
        {
            if (length == -1)
            {
                length = GetLength();
            }

            TEnumType returnValue = ArrayOps.GetEnum <TEnumType>(bytes, currentArrayIndex, length, _propertyInfo.MessagePropertyAttribute.Endianness);

            currentArrayIndex += length;
            return(returnValue);
        }
示例#5
0
        // Called by the generated class when verifying a calculated field
        protected virtual TResultType Deserialize <TResultType>(TypeSerializerBase <TResultType> serializer, byte[] bytes, ref int currentArrayIndex, int length, DeserializeStatus status, params List <byte[]>[] arrays)
        {
            int         startArrayIndex = currentArrayIndex;
            TResultType result          = serializer.Deserialize(bytes, ref currentArrayIndex, length, ref status);
            int         endArrayIndex   = currentArrayIndex;

            byte[] array = ArrayOps.GetSubArray(bytes, startArrayIndex, endArrayIndex - startArrayIndex);
            foreach (List <byte[]> currentArray in arrays)
            {
                currentArray.Add(array);
            }

            return(result);
        }
示例#6
0
        public virtual byte[] Serialize <TListType>(TListType list)
            where TListType : IList, IEnumerable <T>
        {
            byte[] returnArray = new byte[0];
            if ((list != null))
            {
                using (IEnumerator <T> itList = list.GetEnumerator())
                {
                    while (itList.MoveNext())
                    {
                        byte[] currentValueArray = Serialize(itList.Current);
                        returnArray = ArrayOps.Combine(returnArray, currentValueArray);
                    }
                }
            }

            return(returnArray);
        }
示例#7
0
        public string ToString(IMessageSerializable objectToPrint, bool includeBytes, int indentLevel = 0, string separator = null, string bytesSeparator = null, bool putBytesAfter = false, ToStringFormatProperties formatProperties = null)
        {
            MessageSerializedClassInfo classInfo = GetClassInfo(objectToPrint.GetType());

            string stringResult = string.Empty;

            if ((includeBytes && (putBytesAfter == false)))
            {
                stringResult += ArrayOps.GetHexStringFromByteArray(classInfo.Serializer.Serialize(objectToPrint), bytesSeparator) + separator;
            }

            stringResult += classInfo.Serializer.ToString(
                objectToPrint,
                indentLevel,
                formatProperties ?? ToStringFormatProperties.Default);

            if ((includeBytes && putBytesAfter))
            {
                stringResult += separator + ArrayOps.GetHexStringFromByteArray(classInfo.Serializer.Serialize(objectToPrint), bytesSeparator);
            }

            return(stringResult);
        }
示例#8
0
        public override byte[] Serialize(byte[] value)
        {
            // We need to make sure we actually have an array to deal with
            value = value ?? new byte[0];

            // If the field is fixed length then we want to make sure we return that many bytes
            // If the value passed in is too short then additional 0's will be added at the end
            // If the passed in value is too long then it will be cut off at Length bytes
            // For variable length fields we want to check the MinLength and MaxLength and make sure
            // the result matches those using the same rules.  Note that MinLength defaults to 0
            // and MaxLength defaults to -1 (meaning there isn't a max length)
            int maxBytesToGet = value.Length;
            int minBytesToReturn;

            if (_propertyInfo.IsVariableLength)
            {
                if (_propertyInfo.MessagePropertyAttribute.MaxLength != -1)
                {
                    maxBytesToGet = Math.Min(maxBytesToGet, _propertyInfo.MessagePropertyAttribute.MaxLength);
                }

                minBytesToReturn = _propertyInfo.MessagePropertyAttribute.MinLength;
            }
            else
            {
                minBytesToReturn = _propertyInfo.MessagePropertyAttribute.Length;
                maxBytesToGet    = Math.Min(minBytesToReturn, value.Length);
            }

            byte[] serializedValue = ArrayOps.GetSubArray(value, 0, maxBytesToGet);
            if (serializedValue.Length < minBytesToReturn)
            {
                Array.Resize(ref serializedValue, minBytesToReturn);
            }

            return(serializedValue);
        }
示例#9
0
 public override byte[] Deserialize(byte[] bytes, ref int currentArrayIndex, int length, ref DeserializeStatus status)
 {
     byte[] deserializedValue = ArrayOps.GetSubArray(bytes, currentArrayIndex, length);
     currentArrayIndex += length;
     return(deserializedValue);
 }
示例#10
0
 public override byte[] Serialize(TEnumType value)
 {
     return(ArrayOps.GetBytesFromEnum(value, _propertyInfo.MessagePropertyAttribute.Endianness));
 }
示例#11
0
 // TODO: Change startIndex to ref currentIndex and increment (maybe)
 protected ulong GetValueFromBcdArray(byte[] bcdArray, int startIndex, int length)
 {
     byte[] subArray = ArrayOps.GetSubArray(bcdArray, startIndex, length);
     return(GetValueFromBcdArray(subArray));
 }
示例#12
0
 public override byte[] Serialize(DateTime value)
 {
     // TODO: Make this work without having to convert to a numeric or at the very least support longer values
     return(ArrayOps.GetBcdBytes(System.Convert.ToUInt64(value.ToString(_propertyInfo.MessagePropertyAttribute.Format)), GetOutputLength()));
 }