コード例 #1
0
        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();
        }
コード例 #2
0
        private void ConvertInt()
        {
            try
            {
                BigInteger input        = 0;
                var        input_radio  = InputSelector.Children.OfType <RadioButton>().FirstOrDefault(i => i.IsChecked.Value);
                var        output_radio = OutputSelector.Children.OfType <RadioButton>().FirstOrDefault(i => i.IsChecked.Value);
                switch (input_radio.Content.ToString())
                {
                case "Decimal":
                    input = Convert.ToInt64(InputNumber.Text, 10);
                    break;

                case "Binary":
                    input = Convert.ToInt64(InputNumber.Text, 2);
                    break;

                case "BCD":
                    input = NumberSystemConversions.BCDBinToDecimal(InputNumber.Text);
                    break;

                case "Octal":
                    input = Convert.ToInt64(InputNumber.Text, 8);
                    break;

                case "Hexa":
                    input = Convert.ToInt64(InputNumber.Text, 16);
                    break;

                case "Custom":
                    input = NumberSystemConversions.FromSystem(InputNumber.Text,
                                                               (int)Math.Round(SourceSystem.Value));
                    break;
                }
                switch (output_radio.Content.ToString())
                {
                case "Decimal":
                    OutputNumber.Text = NumberSystemConversions.ToSystem(input, 10);
                    break;

                case "Binary":
                    OutputNumber.Text = NumberSystemConversions.ToSystem(input, 2);
                    break;

                case "BCD":
                    OutputNumber.Text = NumberSystemConversions.DecimalToBCDBin((long)input);
                    break;

                case "Octal":
                    OutputNumber.Text = NumberSystemConversions.ToSystem(input, 8);
                    break;

                case "Hexa":
                    OutputNumber.Text = NumberSystemConversions.ToSystem(input, 16);
                    break;

                case "Custom":
                    OutputNumber.Text = NumberSystemConversions.ToSystem(input,
                                                                         (int)Math.Round(TargetSystem.Value));
                    break;
                }
            }
            catch (Exception)
            {
                if (OutputNumber != null)
                {
                    OutputNumber.Text = "Conversion Error";
                }
            }
        }