Guest User

main.cpp

a guest
Nov 5th, 2016
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.20 KB | None | 0 0
  1. #include <stdio.h>
  2. #include "buffer.h"
  3.  
  4. int main()
  5. {
  6.     Buffer buffer(1000);
  7.  
  8.     //Source - notice that only the sign (which is the first bit) is different
  9.     float f1 = 1.987654321;
  10.     float f2 = -1.987654321;
  11.     double d1 = 1.987654321;
  12.     double d2 = -1.987654321;
  13.  
  14.     //Destination
  15.     float _f1;
  16.     float _f2;
  17.     double _d1;
  18.     double _d2;
  19.  
  20.     //Serialize
  21.     buffer.tryWriteFloat(f1); // 4 bytes
  22.     buffer.tryWriteFloat(f2); // 4 bytes
  23.     buffer.tryWriteDouble(d1); // 8 bytes
  24.     buffer.tryWriteDouble(d2); // 8 bytes
  25.  
  26.     //Deserialize
  27.     buffer.tryReadFloat(_f1); // 4 bytes
  28.     buffer.tryReadFloat(_f2); // 4 bytes
  29.     buffer.tryReadDouble(_d1); // 8 bytes
  30.     buffer.tryReadDouble(_d2); // 8 bytes
  31.  
  32.     //Display buffer contents
  33.     printf("\nBuffer contents:\n\n");
  34.     for(long long i = 0; i < buffer.Written(); ++i)
  35.     {
  36.         if(i != 0 && i % 8 == 0 ) printf(" "); // separate after 8 bytes
  37.         printf("%02x", buffer.GetArray()[i]);
  38.     }
  39.     printf("\n\n");
  40.  
  41.     //Values of deserialized variables
  42.     printf("\nValues:\n\n");
  43.     printf("%f\n", _f1);
  44.     printf("%f\n", _f2);
  45.     printf("%lf\n", _d1);
  46.     printf("%lf\n", _d2);
  47.  
  48.     return 0;
  49. }
Advertisement
Add Comment
Please, Sign In to add comment