Esempio n. 1
0
        // Serialization of UInt32
        public static byte[] SerializeUInt32(UInt32 valueToSerialize)
        {
            // 1 or 2 bits for config (lsb)
            // x1 - stored in this byte (on rest 7 bits)
            // 00 - has default value 0
            // 10 - stored in next few bytes - then next 2 bits says on how many bytes the value has been stored


            // Case: 00 - default value
            if (valueToSerialize == 0)
            {
                return new byte[1] {
                           0
                }
            }
            ;

            // Case 1 - value can be stored in configuration byte (on 7 bits => range 1-128 encoded as 0-127)
            if (valueToSerialize > 0 && valueToSerialize < 129)
            {
                return(new byte[1] {
                    (byte)(0x01 | (byte)((valueToSerialize - 1)) << 1)
                });
            }

            // Case 01 - stored in next few bytes - then next 2 bits says on how many bytes the value has been stored
            List <byte> result = new List <byte>(); // Buffer for data

            // Encode value
            byte[] encodedValue = BitToolkit.ConvertInt32ToByteArray((Int32)valueToSerialize);

            // Prepare config byte
            byte config = (byte)(0x02 | (byte)((encodedValue.Length + 1)) << 2);

            // Store values in buffer
            result.Add(config);
            result.AddRange(encodedValue);

            // Return result
            return(result.ToArray());
        }
Esempio n. 2
0
        // Serialization
        public void Serialize(Int32 valueToSerialize)
        {
            // Is it default value
            if (valueToSerialize == 0)
            {
                SerializerStorage.WriteStorageFormat(new DefaultValue());
                return;
            }

            // Should we store value in main data stream?
            if (valueToSerialize > 0 && valueToSerialize <= ValueInConfig.MaxValueToStoreInConfig)
            {
                // We can store value in config
                SerializerStorage.WriteStorageFormat(new ValueInConfig(valueToSerialize));
            }
            else
            {
                byte[] packedData = BitToolkit.ConvertInt32ToByteArray(valueToSerialize);
                SerializerStorage.WriteStorageFormat(new ValueInDataStream((byte)packedData.Length));
                SerializerStorage.WritePackedData(packedData);
            }
        }