6

I am new to this site, and I don't know if I am providing enough info - I'll do my best =)

If you use Notepad++, then you will know what I am talking about -- When a user loads a .exe into Notepad++, the NUL / \x0 character is replaced by NULL, which has a black background, and white text. I tried pasting it into Visual Studio, hoping to obtain the same output, but it just pasted some spaces...

Does anyone know if this is a certain key-combination, or something? I would like to put the NULL character in replacement of \x0, just like Notepad++ =)

This picture contains the NUL character I want :)

Pavel Anikhouski
  • 21,776
  • 12
  • 51
  • 66
  • Use a hex editor instead. – Syed Ahmed Jamil Nov 04 '19 at 08:17
  • Well, I am creating a `Text Editor`, and I am wondering how I would be able to replace `NUL` bytes with the same character `Notepad++` uses :) –  Nov 04 '19 at 08:19
  • 1
    I don't want to use a `hex` editor -- I am trying to create my own type of app =) –  Nov 04 '19 at 08:20
  • I see. Read my answer below. See if that helps. – Syed Ahmed Jamil Nov 04 '19 at 08:54
  • It is _extremely_ not advised to put 0x00 bytes in a text. It is not a valid character in any encoding system, and c/c++ programs even automatically consider it the end of a string. It is _also_ extremely not advised to open an exe file in a text editor. Just use a hex editor, sheesh. You can't do anything good there anyway. – Nyerguds Nov 04 '19 at 10:01
  • Is this an [X-Y problem](https://meta.stackexchange.com/questions/66377/what-is-the-xy-problem)? Perhaps you can explain what you are really trying to do rather than asking why your attempt at a solution is not working. – AdrianHHH Nov 04 '19 at 12:45
  • Well, I want to display the `NUL` byte in my `RichTextBox`, the same way Notepad++ does :) –  Nov 04 '19 at 22:36

4 Answers4

3

Notepad++ may use custom or special fonts to show these particular characters. This behavior also may not appropriate for all text editors. So, they don't show them.
If you want to write a text editor that visualize these characters, you probably need to implement this behavior programmatically. Seeing notepad++ source can be helpful If you want.

  • I checked it, but it is written in C++ :( I don't know C++ very well.. –  Nov 04 '19 at 22:32
  • 1
    It's not going to be an easy task. To do this, you may need to do a lot of low-level programming and even create custom text box control, which needs a lot of effort!. – Farhad Rahmanifard Nov 05 '19 at 05:14
3

Notepad++ is a rich text editor unlike your regular notepad. It can display custom graphics so common in all modern text editors. While reading a file whenever notepad++ encounters the ASCII code of a null character then instead of displaying nothing it adds the string "NULL" to the UI setting the text background colour to black and text colour to white which is what you are seeing. You can show any custom style in your rich text editor too.

NOTE: This is by no means an efficient solution. I'm clearly traversing a read string 2 times just to take benefit of already present methods. This can be done manually in a single pass. It is just to give a hint about how you can do it. Also I wrote the code carefully but haven't ran it because I don't have the tools at the moment. I apologise for any mistakes let me know I'll update it

Step 1 : Read a text file by line (line ends at '\n') and replace all instances of null character of that line with the string "NUL" using the String.Replace(). Finally append the modified text to your RichTextBox.

Step 2 : Re traverse your read line using String.IndexOf() finding start indexes of each "NUL" word. Using these indexed you select text from RichTextBox and then style that selected text using RichTextBox.SelectionColor and RichTextBox.SelectionBackColor

richTextBoxCursor basically just represents the start index of each line in RichTextBox

StreamReader sr = new StreamReader(@"c:\test.txt" , Encoding.UTF8); 
int richTextBoxCursor = 0;

while (!sr.EndOfStream){

   richTextBoxCursor = richTextBox.TextLength;

   string line = sr.ReadLine();
   line = line.Replace(Convert.ToChar(0x0).ToString(), "NUL");
   richTextBox.AppendText(line);

   i = 0;

   while(true){
     i = line.IndexOf("NUL", i) ;
     if(i == -1) break;

     // This specific select function select text start from a certain start index to certain specified character range passed as second parameter
     // i is the start index of each found "NUL" word in our read line
     // 3 is the character range because "NUL" word has three characters
     richTextBox.Select(richTextBoxCursor + i , 3);

     richTextBox.SelectionColor = Color.White;
     richTextBox.SelectionBackColor = Color.Black;

     i++;
   }

}
Syed Ahmed Jamil
  • 1,881
  • 3
  • 18
  • 34
  • How would I do that? –  Nov 04 '19 at 22:33
  • Well it depends what tools are you using ? WinForms, WPF or something else ? – Syed Ahmed Jamil Nov 05 '19 at 00:39
  • I am using `Windows Forms`, and I am programming with my `RichTextBox`, using `Visual Studio` –  Nov 06 '19 at 02:15
  • See my edited answer. Main thing is to select each "NUL" word in `RichTextBox` using `RichTextBox.Select()` method and color its text color and background color using `RichTextBox.SelectionColor` and `RichTextBox.SelectionBackColor` – Syed Ahmed Jamil Nov 06 '19 at 17:07
  • You can also follow these links that make use of Regex to make your life easier https://stackoverflow.com/questions/3707120/how-to-select-text-from-the-richtextbox-and-then-color-it , https://www.codeproject.com/Articles/10675/Enabling-syntax-highlighting-in-a-RichTextBox , https://www.c-sharpcorner.com/article/search-and-highlight-text-in-rich-textbox/ – Syed Ahmed Jamil Nov 06 '19 at 17:11
2

Text editor

As far as I know in order to make Visual Studio display non printable characters you need to install an extension from the marketplace at https://marketplace.visualstudio.com.

One such extension, which I have neither tried nor recomend - I just did a quick search and this is the first result - is Invisible Character Visualizer.

Having said that, copy-pasting binaries is a risky business.


You may try Edit > Advanced > View White Space first.


Binary editor

To really see what's going on you could use the VS' binary editor: File->Open->(Open with... option)->Binary Editor -> OK

enter image description here enter image description here enter image description here

tymtam
  • 31,798
  • 8
  • 86
  • 126
  • So, that being said, if I did just paste the `NUL` symbol into Visual Studio, I would still get the same output in a label, even if the string was made up of spaces? I pasted the `NUL` bytes, and I am getting random spaces instead -- Will that affect anything? Or will it just print spaces? :D –  Nov 04 '19 at 08:46
  • I'd say that the only way to be sure if the characters are preserved is to use the binary editor. – tymtam Nov 04 '19 at 09:29
1

To answer your question.

It's a symbolic representation of 00H double byte.

You're copying and pasting the values. Notepad++ is showing you symbols that replace the representation of those values (because you configured it to do so in that IDE).

Brett Caswell
  • 1,486
  • 1
  • 13
  • 25
  • But, how can I display the SAME symbols? Using the `00H` double `byte`? –  Nov 04 '19 at 08:33
  • In regards to your replicating the concept, would have to research into what notepad++ is doing - since that feature isn't restricted to specific fonts as it relates to styles. The Glpyh isn't likely to be in a character map of the font used by the rest of the page, that is. – Brett Caswell Nov 04 '19 at 08:34
  • Hmm.. How would I do this? I see `Notepad++` on `Github`, but I have no idea what's going on there -- I know `C#`like crazy, but `C++`, a mystery.. –  Nov 04 '19 at 08:38
  • well.. with just brief review of the code, it's possible they are using "transliteration" as a means. charMap source and charMap destination. Typically a concept for languages, but could apply to this feature. – Brett Caswell Nov 04 '19 at 08:45
  • :) I will look into that after I am done checking out `Invisible Character See-er` or something :D –  Nov 04 '19 at 08:48
  • actually.. they are handling everything about the rendering in the GTK, to include creating fonts. [github link](https://github.com/notepad-plus-plus/notepad-plus-plus/blob/master/scintilla/gtk/PlatGTK.cxx#L74). I'd say, perhaps review the GTK source there and see how much C++ font rending concepts you can absorb from it, then perhaps pose a question to the C++ members of this community in your earnest attempt to handle such a feature. – Brett Caswell Nov 04 '19 at 08:56
  • - Actually, I have an idea - Could I just, scan for the `\x0` `NUL` bytes, and then obviously replace them with `NUL`, but I would change the selection backcolor of the `NUL` bytes to black, and the selection color to white. That could work? :D –  Nov 04 '19 at 22:35