Advertisement
Caminhoneiro

Converting base Types to binary and converting back

Jul 17th, 2018
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.86 KB | None | 0 0
  1. [TestClass]
  2.     public class ConvertingBaseTypesToBinaryExample
  3.     {
  4.  
  5.         [TestMethod]
  6.         public void BasicTypes()
  7.         {
  8.             const float originalFloat = 24.56f;
  9.  
  10.             byte[] byteArray = BitConverter.GetBytes(originalFloat);
  11.  
  12.             float convertedBack = BitConverter.ToSingle(byteArray, 0);
  13.         }
  14.  
  15.  
  16.  
  17.  
  18.  
  19.  
  20.  
  21.  
  22.  
  23.  
  24.         [TestMethod]
  25.         public void DateTimes()
  26.         {
  27.             DateTime originalDate = DateTime.Now;
  28.  
  29.  
  30.             long originalLong = originalDate.ToBinary();
  31.  
  32.             byte[] byteArray = BitConverter.GetBytes(originalLong);
  33.  
  34.  
  35.  
  36.             DateTime convertedBack = DateTime.FromBinary(
  37.                                                                          BitConverter.ToInt64(byteArray, 0));
  38.  
  39.             var isSameDate = originalDate == convertedBack;
  40.         }
  41.     }
  42. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement