Exemple #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);
        }
Exemple #2
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);
        }
Exemple #3
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);
        }
Exemple #4
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);
 }
Exemple #5
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));
 }