Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class SerializationSample
- {
- private static MemoryStream ms = null;
- private static BinaryFormatter bf = null;
- static void Main()
- {
- decimal[] theArray = new decimal[2];
- theArray[0] = 36300288.734436237M;
- theArray[1] = 34176522.502899267M;
- bf = new BinaryFormatter();
- ms = new MemoryStream();
- // Convert to Byte array.
- byte[] theBytes = GetBytes(theArray);
- // Convert to Decimal array.
- decimal[] theDecArray = GetDecimals(theBytes);
- }
- private static byte[] GetBytes(decimal[] theArray)
- {
- bf.Serialize(ms, theArray);
- return ms.GetBuffer();
- }
- private static decimal[] GetDecimals(byte[] theArray)
- {
- try
- {
- if (ms.CanSeek)
- ms.Seek(0, SeekOrigin.Begin);
- return (decimal[])bf.Deserialize(ms);
- }
- catch (InvalidCastException)
- {
- // Do something.
- }
- }
- }
Add Comment
Please, Sign In to add comment