Cerebrus

Cerebrus

Nov 25th, 2009
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.89 KB | None | 0 0
  1. class SerializationSample
  2. {
  3.   private static MemoryStream ms = null;
  4.   private static BinaryFormatter bf = null;
  5.  
  6.   static void Main()
  7.   {
  8.     decimal[] theArray = new decimal[2];
  9.     theArray[0] = 36300288.734436237M;
  10.     theArray[1] = 34176522.502899267M;
  11.  
  12.     bf = new BinaryFormatter();
  13.     ms = new MemoryStream();
  14.    
  15.     // Convert to Byte array.
  16.     byte[] theBytes = GetBytes(theArray);
  17.    
  18.     // Convert to Decimal array.
  19.     decimal[] theDecArray = GetDecimals(theBytes);
  20.   }
  21.  
  22.   private static byte[] GetBytes(decimal[] theArray)
  23.   {
  24.     bf.Serialize(ms, theArray);
  25.     return ms.GetBuffer();
  26.   }
  27.  
  28.   private static decimal[] GetDecimals(byte[] theArray)
  29.   {
  30.     try
  31.     {
  32.       if (ms.CanSeek)
  33.         ms.Seek(0, SeekOrigin.Begin);
  34.  
  35.       return (decimal[])bf.Deserialize(ms);
  36.     }
  37.     catch (InvalidCastException)
  38.     {
  39.       // Do something.
  40.     }
  41.   }
  42. }
Add Comment
Please, Sign In to add comment