HiveBrain v1.2.0
Get Started
← Back to all entries
patterncsharpMinor

A Binary Editor with Live Text Preview

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
previewwithtexteditorlivebinary

Problem

This is a tool I needed for some other things (it's incomplete, but almost done) which is essentially just a text-editor that has two text-boxes: the left being the binary view / raw bytes, the right being the text when the bytes are decoded with the specified Encoding.

The only features it doesn't yet have that I need to add are Open, Save, and New for the most part. (Maybe a couple others that may be handy, but not necessary right now.)

It's entirely in WPF / XAML / C# using MVVM for everything.

This first bit is the MainWindow.xaml.cs, there's almost nothing to this part except setting up the DataContext:

namespace BinaryEditor
{
    /// 
    /// Interaction logic for MainWindow.xaml
    /// 
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            DataContext = new MainWindowViewModel();
        }
    }
}


Next, we have the MainWindow.xaml which is obviously where all the XAML happens:

```



































































Solution

I've spotted a few minor issues in your code.

You might consider encapsulating these formulas:

Math.Ceiling(valueAsString.Length / 2.0)


and

valueAsString.Length * 1.5


and

i * 2 + 1


because they appear in multiple places namely the ByteStringConverter and the MainWindowViewModel - both use them.

SelectedFontString


I think this property should be called SelectedFontFamily because you bind it to the FontFamily property in XAML.

switch (SelectedEncoding)
            {
                case 1:
                    return string.Join("", Encoding.UTF8.GetString(bytes));
                case 2:
                    return string.Join("", Encoding.Unicode.GetString(bytes));
                case 3:
                    return string.Join("", Encoding.UTF32.GetString(bytes));
                default:
                    return string.Join("", Encoding.ASCII.GetString(bytes));
            }


Those cases aren't very pretty. An enum would be optimal.

var bytes = BytesString;
            _selectedEncoding = value;
            OnPropertyChanged(new PropertyChangedEventArgs(nameof(SelectedEncoding)));
            BytesString = bytes;


This looks suspicious. Shouldn't there be another call to OnPropertyChanged but for the BytesString?

And one more thing...

public void OnPropertyChanged(PropertyChangedEventArgs e)
    {
        var handler = PropertyChanged;
        handler?.Invoke(this, e);
    }


If you already have such method then let it work with a string and create the EventArgs inside this method. The calls to OnPropertyChagned will be much shorter like OnPropertyChanged(nameof(BytesString));

Code Snippets

Math.Ceiling(valueAsString.Length / 2.0)
valueAsString.Length * 1.5
SelectedFontString
switch (SelectedEncoding)
            {
                case 1:
                    return string.Join("", Encoding.UTF8.GetString(bytes));
                case 2:
                    return string.Join("", Encoding.Unicode.GetString(bytes));
                case 3:
                    return string.Join("", Encoding.UTF32.GetString(bytes));
                default:
                    return string.Join("", Encoding.ASCII.GetString(bytes));
            }
var bytes = BytesString;
            _selectedEncoding = value;
            OnPropertyChanged(new PropertyChangedEventArgs(nameof(SelectedEncoding)));
            BytesString = bytes;

Context

StackExchange Code Review Q#146240, answer score: 4

Revisions (0)

No revisions yet.