public void DisplayNumber(object o) { if (Helpers.IsSpecialType(o)) { Text = "Special types are not supported for conversion"; return; } double d = Convert.ToDouble(o); float f = Convert.ToSingle(o); bool floats = (d - Math.Truncate(d)) != 0; var buffer = new StringBuilder(); if (floats) { byte[] singlebytes = BitConverter.GetBytes(f); Array.Reverse(singlebytes); byte[] doublebytes = BitConverter.GetBytes(d); Array.Reverse(doublebytes); buffer.AppendFormat("IEEE 754 Double: {0}\r\n", NumberSystemConversions.ByteArrayToHex(doublebytes)); buffer.AppendFormat("IEEE 754 Single: {0}", NumberSystemConversions.ByteArrayToHex(singlebytes)); Text = buffer.ToString(); return; } else { string bin, oct, hex; var bi = new BigInteger(d); bin = NumberSystemConversions.ToSystem(bi, 2); oct = NumberSystemConversions.ToSystem(bi, 8); hex = NumberSystemConversions.ToSystem(bi, 16); int bits = bin.Length; bin = NumberSystemConversions.FormatBin(bin); buffer.AppendFormat("DEC: {0}\n", bi); buffer.AppendFormat("BIN: {0}\n", bin); buffer.AppendFormat("OCT: {0}\n", oct); buffer.AppendFormat("HEX: {0}\n", hex); buffer.AppendFormat("-------------------------------------\n"); buffer.AppendFormat("Bits: {0}", bits); } Text = buffer.ToString(); }
private void ConvertIEEE754(string text) { try { var buffer = new StringBuilder(); float single = Convert.ToSingle(text); double d = Convert.ToDouble(text); byte[] singlebytes = BitConverter.GetBytes(single); Array.Reverse(singlebytes); byte[] doublebytes = BitConverter.GetBytes(d); Array.Reverse(doublebytes); string singlebin = NumberSystemConversions.ByteArrayToBin(singlebytes); string singlehex = NumberSystemConversions.ByteArrayToHex(singlebytes); string doublebin = NumberSystemConversions.ByteArrayToBin(doublebytes); string doublehex = NumberSystemConversions.ByteArrayToHex(doublebytes); buffer.AppendFormat("Hexadecimal single value: {0}\n", singlehex); buffer.AppendFormat("Binary single value: {0}\n", singlebin); buffer.AppendFormat("Sign: {0}\n", singlebin.Substring(0, 1)); buffer.AppendFormat("Exponent: {0}\n", singlebin.Substring(1, 8)); buffer.AppendFormat("Fraction: {0}\n", singlebin.Substring(8, 23)); buffer.Append("--------------------------------------------------\n"); buffer.AppendFormat("Hexadecimal double value: {0}\n", doublehex); buffer.AppendFormat("Binary double value: {0}\n", doublebin); buffer.AppendFormat("Sign: {0}\n", doublebin.Substring(0, 1)); buffer.AppendFormat("Exponent: {0}\n", doublebin.Substring(1, 11)); buffer.AppendFormat("Fraction: {0}\n", doublebin.Substring(11, 52)); IEEE754Output.Text = buffer.ToString(); } catch (Exception) { IEEE754Output.Text = "Input error"; } }