示例#1
0
        static StackPanel ShowNumericForms(object value, NumericForm form)
        {
            if (value == null)
            {
                return(null);
            }
            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.Int32: {
                var v = (int)value;
                if (form == NumericForm.Negative)
                {
                    v = -v;
                }
                return(ShowNumericForms(
                           form == NumericForm.Unsigned ? ((uint)v).ToString() : v.ToString(),
                           new byte[] { (byte)(v >> 24), (byte)(v >> 16), (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.Int64: {
                var v = (long)value;
                if (form == NumericForm.Negative)
                {
                    v = -v;
                }
                return(ShowNumericForms(
                           form == NumericForm.Unsigned ? ((ulong)v).ToString() : v.ToString(),
                           new byte[] { (byte)(v >> 56), (byte)(v >> 48), (byte)(v >> 40), (byte)(v >> 32), (byte)(v >> 24), (byte)(v >> 16), (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.Byte:
                return(ShowNumericForms(((byte)value).ToString(), new byte[] { (byte)value }));

            case TypeCode.Int16: {
                var v = (short)value;
                if (form == NumericForm.Negative)
                {
                    v = (short)-v;
                }
                return(ShowNumericForms(
                           form == NumericForm.Unsigned ? ((ushort)v).ToString() : v.ToString(),
                           new byte[] { (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.Char: {
                var v = (char)value;
                return(ShowNumericForms(((ushort)v).ToString(), new byte[] { (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.UInt32:
                return(ShowNumericForms((int)(uint)value, NumericForm.Unsigned));

            case TypeCode.UInt16:
                return(ShowNumericForms((short)(ushort)value, NumericForm.Unsigned));

            case TypeCode.UInt64:
                return(ShowNumericForms((long)(ulong)value, NumericForm.Unsigned));

            case TypeCode.SByte:
                return(ShowNumericForms(((sbyte)value).ToString(), new byte[] { (byte)(sbyte)value }));
            }
            return(null);
        }
示例#2
0
        static Grid ShowNumericForms(object value, NumericForm form)
        {
            if (value == null)
            {
                return(null);
            }
            switch (Type.GetTypeCode(value.GetType()))
            {
            case TypeCode.Int32: {
                var v = (int)value;
                if (form == NumericForm.Negative)
                {
                    v = -v;
                }
                return(ShowNumberAndBytes(
                           form == NumericForm.Unsigned ? ((uint)v).ToString() : v.ToString(),
                           new byte[] { (byte)(v >> 24), (byte)(v >> 16), (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.Int64: {
                var v = (long)value;
                if (form == NumericForm.Negative)
                {
                    v = -v;
                }
                return(ShowNumberAndBytes(
                           form == NumericForm.Unsigned ? ((ulong)v).ToString() : v.ToString(),
                           new byte[] { (byte)(v >> 56), (byte)(v >> 48), (byte)(v >> 40), (byte)(v >> 32), (byte)(v >> 24), (byte)(v >> 16), (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.Byte:
                return(ShowNumberAndBytes(((byte)value).ToString(), new byte[] { (byte)value }));

            case TypeCode.Int16: {
                var v = (short)value;
                if (form == NumericForm.Negative)
                {
                    v = (short)-v;
                }
                return(ShowNumberAndBytes(
                           form == NumericForm.Unsigned ? ((ushort)v).ToString() : v.ToString(),
                           new byte[] { (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.Char: {
                var v = (char)value;
                return(ShowNumberAndBytes(((ushort)v).ToString(), new byte[] { (byte)(v >> 8), (byte)v }));
            }

            case TypeCode.UInt32:
                return(ShowNumericForms((int)(uint)value, NumericForm.Unsigned));

            case TypeCode.UInt16:
                return(ShowNumericForms((short)(ushort)value, NumericForm.Unsigned));

            case TypeCode.UInt64:
                return(ShowNumericForms((long)(ulong)value, NumericForm.Unsigned));

            case TypeCode.SByte:
                return(ShowNumberAndBytes(((sbyte)value).ToString(), new byte[] { (byte)(sbyte)value }));
            }
            return(null);

            Grid ShowNumberAndBytes(string number, byte[] bytes)
            {
                return(new Grid {
                    HorizontalAlignment = HorizontalAlignment.Left,
                    RowDefinitions =
                    {
                        new RowDefinition(), new RowDefinition(), new RowDefinition()
                    },
                    ColumnDefinitions =
                    {
                        new ColumnDefinition(), new ColumnDefinition {
                            Width = new GridLength(3,GridUnitType.Star)
                        }
                    },
                    Children =
                    {
                        new ThemedTipText(R.T_Decimal,                       true)
                        {
                            Margin = WpfHelper.GlyphMargin,                  TextAlignment      = TextAlignment.Right
                        },
                        new ThemedTipText(R.T_Hexadecimal,                   true)
                        {
                            Margin = WpfHelper.GlyphMargin,                  TextAlignment      = TextAlignment.Right
                        }.SetValue(Grid.SetRow,                        1),
                        new ThemedTipText(R.T_Binary,                        true)
                        {
                            Margin = WpfHelper.GlyphMargin,                  TextAlignment      = TextAlignment.Right
                        }.SetValue(Grid.SetRow,2),
                        new ThemedTipText(number)
                        {
                            Background = ThemeHelper.TextBoxBackgroundBrush.Alpha(0.5),Foreground         = ThemeHelper.TextBoxBrush,                      Padding = WpfHelper.SmallHorizontalMargin
                        }.WrapBorder(ThemeHelper.TextBoxBorderBrush,         WpfHelper.TinyMargin).SetValue(Grid.SetColumn, 1),
                        ToHexString(new ThemedTipText()
                        {
                            Background = ThemeHelper.TextBoxBackgroundBrush.Alpha(0.5),Foreground         = ThemeHelper.TextBoxBrush,                      Padding = WpfHelper.SmallHorizontalMargin
                        },                                                   bytes).WrapBorder(ThemeHelper.TextBoxBorderBrush, WpfHelper.TinyMargin).SetValue(Grid.SetColumn, 1).SetValue(Grid.SetRow, 1),
                        ToBinString(new ThemedTipText()
                        {
                            Background = ThemeHelper.TextBoxBackgroundBrush.Alpha(0.5),Foreground         = ThemeHelper.TextBoxBrush,                      Padding = WpfHelper.SmallHorizontalMargin
                        },                                                   bytes).WrapBorder(ThemeHelper.TextBoxBorderBrush, WpfHelper.TinyMargin).SetValue(Grid.SetColumn, 1).SetValue(Grid.SetRow, 2),
                    }
                });
            }

            ThemedTipText ToBinString(ThemedTipText text, byte[] bytes)
            {
                var inlines = text.Inlines;

                inlines.Add(new Run("0b")
                {
                    FontWeight = FontWeights.Bold
                });
                var hasValue = false;

                for (int i = 0; i < bytes.Length; i++)
                {
                    ref var b = ref bytes[i];
                    if (hasValue || b != 0)
                    {
                        hasValue = true;
                        inlines.Add(Convert.ToString(b, 2).PadLeft(8, '0'));
                        if ((i & 1) == 1)
                        {
                            AddBackground(inlines);
                        }
                    }
                }
                return(hasValue ? text : text.Append("00000000"));
            }