I have a requirement to be able to parse a special NaN byte array to determine a specific condition. The array is 0x0000A07F. However, when I run this through BitConverter and cast to a float and then back to a byte array, the conversion does not match.
private void testNan()
{
Single tf;
Byte[] to;
Byte[] ti;
// Normal NaN
Debug.WriteLine("");
Debug.Write("Normal NaN: 0x");
tf = Single.NaN;
to = BitConverter.GetBytes(tf);
foreach (var cb in to)
Debug.Write(cb.ToString("X2"));
// Special NaN
Debug.WriteLine("");
Debug.Write("Special NaN: 0x");
ti = new byte[] { 0x00, 0x00, 0xA0, 0x7F }; // Special Array
tf = BitConverter.ToSingle(ti, 0); // Float Conversion
to = BitConverter.GetBytes(tf); // Byte Conversion
foreach (var cb in to)
Debug.Write(cb.ToString("X2"));
// Iterate Conversion (3rd Byte)
Debug.WriteLine("");
Debug.WriteLine("Test Conversions");
for (var count = 0; count < 256; count++)
{
ti = new byte[] { 0x00, 0x00, (byte) count, 0x7F };
tf = BitConverter.ToSingle(ti, 0);
to = BitConverter.GetBytes(tf);
var pass = true;
for (var subCount = 0; subCount < 4; subCount++)
if (ti[subCount] != to[subCount]) pass = false;
if (pass) continue;
Debug.Write("FAIL [0x" + count.ToString("X2") + "]: ");
Debug.Write("IN=0x");
foreach (var cb in ti)
Debug.Write(cb.ToString("X2"));
Debug.Write(" OUT=0x");
foreach (var cb in to)
Debug.Write(cb.ToString("X2"));
Debug.WriteLine("");
}
}
//Normal NaN: 0x0000C0FF
//Special NaN: 0x0000E07F
//Test Conversions
//FAIL [0x81]: IN=0x0000817F OUT=0x0000C17F
//FAIL [0x82]: IN=0x0000827F OUT=0x0000C27F
//FAIL [0x83]: IN=0x0000837F OUT=0x0000C37F
//FAIL [0x84]: IN=0x0000847F OUT=0x0000C47F
//FAIL [0x85]: IN=0x0000857F OUT=0x0000C57F
//FAIL [0x86]: IN=0x0000867F OUT=0x0000C67F
//FAIL [0x87]: IN=0x0000877F OUT=0x0000C77F
//FAIL [0x88]: IN=0x0000887F OUT=0x0000C87F
//FAIL [0x89]: IN=0x0000897F OUT=0x0000C97F
//FAIL [0x8A]: IN=0x00008A7F OUT=0x0000CA7F
//FAIL [0x8B]: IN=0x00008B7F OUT=0x0000CB7F
//FAIL [0x8C]: IN=0x00008C7F OUT=0x0000CC7F
//FAIL [0x8D]: IN=0x00008D7F OUT=0x0000CD7F
//FAIL [0x8E]: IN=0x00008E7F OUT=0x0000CE7F
//FAIL [0x8F]: IN=0x00008F7F OUT=0x0000CF7F
//FAIL [0x90]: IN=0x0000907F OUT=0x0000D07F
//FAIL [0x91]: IN=0x0000917F OUT=0x0000D17F
//FAIL [0x92]: IN=0x0000927F OUT=0x0000D27F
//FAIL [0x93]: IN=0x0000937F OUT=0x0000D37F
//FAIL [0x94]: IN=0x0000947F OUT=0x0000D47F
//FAIL [0x95]: IN=0x0000957F OUT=0x0000D57F
//FAIL [0x96]: IN=0x0000967F OUT=0x0000D67F
//FAIL [0x97]: IN=0x0000977F OUT=0x0000D77F
//FAIL [0x98]: IN=0x0000987F OUT=0x0000D87F
//FAIL [0x99]: IN=0x0000997F OUT=0x0000D97F
//FAIL [0x9A]: IN=0x00009A7F OUT=0x0000DA7F
//FAIL [0x9B]: IN=0x00009B7F OUT=0x0000DB7F
//FAIL [0x9C]: IN=0x00009C7F OUT=0x0000DC7F
//FAIL [0x9D]: IN=0x00009D7F OUT=0x0000DD7F
//FAIL [0x9E]: IN=0x00009E7F OUT=0x0000DE7F
//FAIL [0x9F]: IN=0x00009F7F OUT=0x0000DF7F
//FAIL [0xA0]: IN=0x0000A07F OUT=0x0000E07F
//FAIL [0xA1]: IN=0x0000A17F OUT=0x0000E17F
//FAIL [0xA2]: IN=0x0000A27F OUT=0x0000E27F
//FAIL [0xA3]: IN=0x0000A37F OUT=0x0000E37F
//FAIL [0xA4]: IN=0x0000A47F OUT=0x0000E47F
//FAIL [0xA5]: IN=0x0000A57F OUT=0x0000E57F
//FAIL [0xA6]: IN=0x0000A67F OUT=0x0000E67F
//FAIL [0xA7]: IN=0x0000A77F OUT=0x0000E77F
//FAIL [0xA8]: IN=0x0000A87F OUT=0x0000E87F
//FAIL [0xA9]: IN=0x0000A97F OUT=0x0000E97F
//FAIL [0xAA]: IN=0x0000AA7F OUT=0x0000EA7F
//FAIL [0xAB]: IN=0x0000AB7F OUT=0x0000EB7F
//FAIL [0xAC]: IN=0x0000AC7F OUT=0x0000EC7F
//FAIL [0xAD]: IN=0x0000AD7F OUT=0x0000ED7F
//FAIL [0xAE]: IN=0x0000AE7F OUT=0x0000EE7F
//FAIL [0xAF]: IN=0x0000AF7F OUT=0x0000EF7F
//FAIL [0xB0]: IN=0x0000B07F OUT=0x0000F07F
//FAIL [0xB1]: IN=0x0000B17F OUT=0x0000F17F
//FAIL [0xB2]: IN=0x0000B27F OUT=0x0000F27F
//FAIL [0xB3]: IN=0x0000B37F OUT=0x0000F37F
//FAIL [0xB4]: IN=0x0000B47F OUT=0x0000F47F
//FAIL [0xB5]: IN=0x0000B57F OUT=0x0000F57F
//FAIL [0xB6]: IN=0x0000B67F OUT=0x0000F67F
//FAIL [0xB7]: IN=0x0000B77F OUT=0x0000F77F
//FAIL [0xB8]: IN=0x0000B87F OUT=0x0000F87F
//FAIL [0xB9]: IN=0x0000B97F OUT=0x0000F97F
//FAIL [0xBA]: IN=0x0000BA7F OUT=0x0000FA7F
//FAIL [0xBB]: IN=0x0000BB7F OUT=0x0000FB7F
//FAIL [0xBC]: IN=0x0000BC7F OUT=0x0000FC7F
//FAIL [0xBD]: IN=0x0000BD7F OUT=0x0000FD7F
//FAIL [0xBE]: IN=0x0000BE7F OUT=0x0000FE7F
//FAIL [0xBF]: IN=0x0000BF7F OUT=0x0000FF7F
I iterated though the conversions and come out with a range of conversions that don't work. Basically if the 8th and 4th bits are set in the third byte, the 7th bit is always set after the conversion.
In other words 8* C* (80 doesn't do it as the error starts at 81) 9* D* A* E* B* F*