Skip to content

Binary Editing

gholmann16 edited this page Jul 28, 2024 · 2 revisions

Binary Editing

As briefly covered in the README.md, Janus has in-built binary editing functionality. In this wiki page I wish to further explain how binary editing works in Janus and explain the reasoning for implementing this feature in the way I have.

In depth explanation

Binary data is data not formatted in a human-readable format. Examples include png, mp4, and zip files. Such file formats encode data in a non-readable way in order to conserve storage space. When loading binary data from a file, Janus takes each byte in a file and individually translates them into Unicode, the universal standard for displaying text. It does so by taking each binary character's ASCII hex code and translating that into a Unicode counterpart. The ASCII table is shown below:

ASCII table from wikipedia

As seen above, the ASCII code for the character 'A' is 0x41, which is to say that it is 41 in hexidecimal. In order to convert this to Unicode, simply replace the "0x" hexidecimal signifier with the "U+" Unicode signifier. For example, when translating 'A', or 0x41, we get U+41, which is indeed the same Unicode character for 'A'. However, as you may notice, the ASCII table only continues until 0x7f, and computers can store up to 0xff, or 256 bits to a byte. Here, the Unicode table, which is much more expansive than ASCII and includes characters from hundreds of languages, can help us. The character code U+80 is valid, as is U+81, going all the way up to U+ff. So when determining how your binary data will be displayed on screen, Janus simply converts the binary number into its Unicode equivalent:

Binary data Unicode Character Code Unicode Character
0x41 U+41 A
0x42 U+42 B
0x43 U+43 C
...
0x7f U+7f
0x80 U+80

The one exception to this rule is the null character. Unfortunately, due to a limitation in glib (and not in UTF-8/Unicode), null characters cannot be displayed in GTK. In place of it, Janus uses the NUL stand-in, U+2400, ␀. All of this explanation might go a little over your head, so here is a practical demonstration of binary editing in Janus:

Gif displaying binary editing in janus

Finally, saving files. Saving a binary files works the same way as opening one, just in reverse. When you save a file, it will run through each character and convert them back into binary. U+42 will return to 0x42, U+d4 will return to 0xd4, and U+2400 will return to 0x00. As long as you haven't added any Unicode characters above character code U+ff, in other words representing an impossible binary character code, your file should save and everything should be fine.

Reasoning

In my opinion, one of most important features of Windows Notepad is its ability to open any kind of file, including files not written in human-readable format. For this reason I have chosen to build a binary editing mode into Janus. However, unlike other text editors which use escape characters (i.e. displaying the binary code after a backslash) to represent binary data or prompt the user to convert the often useless data into Unicode using inapplicable codepages, I have chosen a novel approach. Overall I think this solution has some complicated implementation features, but I think that it has a relatively intuitive design for beginners who simply want to view the insides of say a png or zip file. For more experienced users, this feature also allows inserting characters conveniently via Control + Shift + U, and then the ASCII code you wish to add. All things considered I really like the way it works and use the binary editing functionality frequently.

Hex Editors

As a quick aside, I want to state that Janus is not meant to be a full-fledged hex editor. It will allow you to peek into otherwise unreadable files, and to make small, simple changes, but a hex editor still gives the user much more fine-tuned control. For example, the carriage return and the newline characters are indistinguishable in Janus, so if precision matters, it is the wrong tool for the job.

Clone this wiki locally