A Guide to the Binary Editor
The Binary Editor program allows you to view the individual bytes of
any file (in binary, hex and decimal formats), to display the file
contents as a bitmapped image or play them as audio samples, and to
alter the file contents in a systematic way. (Earlier versions
of the program used an Excel spreadsheet with macros that I
wrote. The present version was written by John Houston, '07.)
This in-class lab exercise will teach you the basics of the
program.
Launching the Program
Double-click on the icon named BinEd.jar. The program will start, and
display thesave/load page.
Loading a File
Click on the button "Load a File" . You will be able to navigate
around your hard disk and select any file to open. When the file
is opened the view will automatically switch to the Binary Editor
page. This shows the contents of the file, byte by byte, giving
each byte's value in binary, hex, decimal, and---if the byte
value is the ASCII code of a character---the assoicated character.
In class I will have you open the text of the short story "The
Tell-tale Heart", by Edgar Allen Poe. Since this is a text file, every
byte is the ASCII code of a character. For non-text files,
byte values greater than 127 will typically appear, and the last column
will display "??", since these values do not have any character
equivalent. Notice that the total number of lines in the display
(which in this case is the number of bytes in the file) is displayed
near the bottom of the left-hand panel.
Modifying File Contents with Formulas
You can modify the contents of the file by entering a
formula in the text area to the
left of the main display. The formula is an instruction to
replace the contents of each line by a new value. Formulas are
built using integer constants (like 123, 7, etc.) and the operation
symbols +,-,*,/,%,M along with parentheses "(" and ")". Three of
these operation symbols need to be explained further: The
symbol "/" denotes division, of course, but it is integer
division, which means that the remainder is thrown away, and only the
whole number quotient is retained. For example, 7/3 evaluates to
2. The operation "%" gives the remainder from such a division,
so that 7%3 evaluates to 1, 6%3 evaluates to 0, and 8%3 evaluates to
2. The operation M computes the maximum of two values, so 3M5 and 5M3
both evaluate to 5.
There are two other elements that go into formulas: The symbol n
or N (you can use either case) means "the number of this line", and if
E is an expression denoting a line number, then [E] denotes "the
value in line E".
1. Now let's see the result of applying some formulas. If
you
enter the formula
97
in the text area, and click the button "Apply to All", then all the
displayed byte values will become 97. If you look in the ASCII column,
you will see that every byte represents the character 'a'. You should
try this out. By
the way, when you do this, the original file on disk is not altered at
all; the changes only affect a temporary copy that is being maintained
by the Binary Editor program.
2. Click Undo to restore the original
values. Then enter the formula
N
and click "Apply to All". In principle, the value in every line
will be replaced by the number of the line. But actually this
creates a problem: Since bytes can only have values in the range
0 to 255, the program will not be able to follow this instruction
correctly once it reaches line number 256. The rule it follows
is: if the computed value of a formula is greater than 255, make the
line's value 255.
3. Click Undo again, then enter the formula
1000-N.
You should see that the number of lines in the display has changed.
The program will not display a negative value, and as soon as it
encounters one, it ignores the rest of the data. If you wanted to
achieve an effect like the one in the previous experiment, replacing
all negative values by 0, click Undo, and try
(1000-N) M 0
4.Click Undo and now try out the formula
N%56,
and click "Apply to All". Now line N contains the remainder N
leaves upon division by 56. (This would be a good time to check out the
"Play Sound" feature, but we'll get to that later.)
5.Click Undo
and try out the formula
[N]+7
and click "Apply to "All". This replaces the value in line
N by 7 plus the value in line N---in other words, it adds 7 to every
byte in the file.
6. Click Undo and try
[N+7].
This replaces the value in line N by the value in line N+7. Thus
the contents of line 0 will be replaced by the contents of line 7, the
contents of line 1 by the contents of line 8, etc. What
happens when we get to the line that is just 6 lines from the end of
the file? Then line N+7 has no content, so the program
finishes. The result of applying this formula is to remove the
first 7 bytes of the file---this is a very useful trick.
7. Repeat the above experiments with the formulas
[2*N], 2*[N], [N/2] and [N]/2
Try to guess the effect of the formula before trying it out.
8. Now we'll perform a useful transformation of the text
file.
Click Undo again. If you look at the tables of ASCII codes,
you'll notice that the lower case letters have codes 97 to 122, and the
upper case letters 65 to 90. So if we want to convert all the
lower case letters to upper case, we would apply the formula
[N]-32.
But if we apply this formula to all the cells, then all the byte values
will decrease by 32, not just those representing lower-case letters.
Instead of clicking the Apply to All button, click the button Apply...,
and specify that you want to apply this transformation only to cells
whose values are in the range 97 to 122.
Saving the Result
9. Now go back to the Load/Save view and click "Save as Binary
File". You can save the result under whatever name you
wish. If, after saving, you open the new file in the text editor,
you will see that the case has been converted successfully.
Creating a New File with Formulas
10. We can use the program to create new files
instead of modifying existing ones. Go back to the editor view and
click "Clear Entire Buffer". Only one line---Line 0 will be
visible. Enter the formula
N+32
and click Apply..., then select the option to apply this formula to all
cells whose line numbers lie in the range 0 to 94. This will
create 95 lines, which contain the ASCII codes for the printable
characters. Save the result and view it in the text editor.
11. Open the Text Editor, type the line
Cursive writing does not mean what I think it does.
Bart Simpson has to write this on the board 1000 times. You can
help Bart by saving the file, loading it in the Binary Editor and
applying the formula
[N%52]
to all lines in the range 0 to 52000. Can you think of
a way to do this with a minimum of effort using the TextEditor
alone?
|