Search code examples
c#wpffontsfixed-widthhex-editors

WPF Fixed width Textbox font (Like Hex Editor)


I am building a file parsing tool in WPF that let's me adjust line length till data lines up. See this video around 2:10 https://www.youtube.com/watch?v=OMeghA82kSk

I really need to fix it so that the text has a fixed width. I had thought about maybe doing a DataGridView and having each cell be a character, but that seems slow and kinda silly. Since it is recreating the view constantly, it needs to perform rather quickly.

I feel like what I am asking isn't that unusual, but I have tried using all the Fixed Width fonts, but when it gets to the out of normal range control chars, it doesn't hold up.

I see other applications such as v64 that do exactly what I am looking for (see below). Do I need to use something other than a TextBox? What would be the ideal way to do this?

enter image description here


Solution

  • Ok, so I found the issue. First off, you HAVE to specify the file encoding or else it will skip some bytes. In my case it was skipping \x86 which threw everything off.

    The only way I figured that out was by doing:

    string shortText = File.ReadAllText("Original.dat"); 
    File.WriteAllText("New.dat", shortText); 
    

    And then doing a byte by byte analysis. The right way is to do the following:

    string shortText = File.ReadAllText("Wrapped.dat", Encoding.ASCII); 
    

    Even then, and even with a monospaced font it won't look correct. That is because most TTF fonts don't have a definition for things that aren't alphanumeric, so you add in a regular expression to strip out the rest and it works.

    shortText = Regex.Replace(shortText, @"[^\w\n',|\.@-]", " ", RegexOptions.None);