// Serialization of Int64 public static byte[] SerializeInt64(Int64 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.ConvertInt64ToByteArray(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()); }
// Serialization public void Serialize(Int64 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.ConvertInt64ToByteArray(valueToSerialize); SerializerStorage.WriteStorageFormat(new ValueInDataStream((byte)packedData.Length)); SerializerStorage.WritePackedData(packedData); } }