The Makecode Editor attempts to embed a compressed version of the original program source code into the .hex file, if there is space for it. This is so that if you drag and drop a .hex file back into the makecode.microbit.org editor it will reconstruct your source program for you to allow you to view and change it. You can find more infomation on the fomat and method on our developer site.
Sometimes it is useful to be able to 'lock' a .hex file, perhaps you have written a program that you want students to download from the web or from your intranet and load it onto the micro:bit to work out what it does, but you don't want them to be able to 'cheat' and drag and drop the .hex file into the makecode.microbit.org editor to view the original source program.
How to lock a .hex file
- Open the .hex file in a text editor such as Notepad on windows, or TextEdit on a Apple Mac. This is an intel-hex format file, the format of which is described in the links below. Basically each line has a short header, and the last two characters of the line are a computed checksum formed by adding up all the data on that line.
- Search for the string 41140E2FB82FA2BB using the search feature of your text editor. If you can't find this magic string, try searching for a shorter part of it such as 2FB82FA2BB, as it *might* span multiple lines.
- Find the '41' part and change it to '40' - this reduces the total sum of the line by one, so...
- at the end of that line, look at the last two characters. This is a hex number. Add one to it to make sure that the checksum is now correct.
- Save the file, then try and drag and drop it into makecode.microbit.org - it should now be impossible for Makecode Editor to read it. It will pop up an orange message saying 'Sorry we could not recognize this file'.
- Drag and drop the file onto the MICROBIT drive, and make sure your code works.
The attached hex file microbit-secret.hex has been locked, try dragging and dropping it into pxt.microbit.org and you will see that it will not load it.
Here is how we locked that .hex file. First we searched for the magic string - in this case the :10FCC000 before it is part of the memory address information that appears at the start of every intel-hex line.
Then, we changed the 41 to a 40 (thus reducing the sum of the line by one) and added one to the 30 checksum at the end, making it 31.
Note, if you update the checksum incorrectly, the file will not load properly onto the micro:bit, so make sure you test it on your micro:bit too before distributing it.
When you modify the last two characters of the line to add one to the checksum, this is a hexadecimal number, and it must be updated cyclically. So, here are some examples of how to 'add one' to a 2 digit (8 bit) hex number
30 becomes 31
39 becomes 3A
3F becomes 40
FF becomes 00