Skip to content

Instantly share code, notes, and snippets.

@nycdotnet
Last active August 29, 2015 14:07
Show Gist options
  • Save nycdotnet/f28b7eb3af56d4549936 to your computer and use it in GitHub Desktop.
Save nycdotnet/f28b7eb3af56d4549936 to your computer and use it in GitHub Desktop.
Double as binary
Testing 0
0-0-0-0-0-0-0-0-
After rehydrate: 0
Testing 1
0-0-0-0-0-0-240-63-
After rehydrate: 1
Testing -1
0-0-0-0-0-0-240-191-
After rehydrate: -1
Testing NaN
0-0-0-0-0-0-248-255-
After rehydrate: NaN
Testing Infinity
0-0-0-0-0-0-240-127-
After rehydrate: Infinity
Testing -Infinity
0-0-0-0-0-0-240-255-
After rehydrate: -Infinity
using System;
using System.Diagnostics;
using System.Text;
namespace DoubleTest
{
class Program
{
static void Main(string[] args)
{
Debug.Print("Start at " + DateTime.Now);
testValue(0d);
testValue(1d);
testValue(-1d);
testValue(Double.NaN);
testValue(Double.PositiveInfinity);
testValue(Double.NegativeInfinity);
}
static void testValue(Double valueToTest)
{
double theVal = valueToTest;
Debug.Print("Testing " + theVal.ToString());
byte[] theBytes = BitConverter.GetBytes(theVal);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < theBytes.Length; i += 1)
{
sb.Append(theBytes[i].ToString() + "-");
}
Debug.Print(sb.ToString());
double rehydratedValue = BitConverter.ToDouble(theBytes, 0);
Debug.Print("After rehydrate: " + rehydratedValue.ToString());
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment