We now have a way that our program can react to the events. What we need next is a way that we can store the dots and dashes and use the stored information to identify a particular code.

We could think of dot as meaning 1 and dash as meaning 0. This means that I could represent a morse code character in a single byte, as long as it doesn't contain more than 8 items. For example, the letter A is dot dash.

A = binary 10 - which is decimal 2

The snag with just using bits in this way is that I also need to know where the dots and dashes end, for example I would not be able to tell A (dot dash) from Q (dash dash dot dash):

A = binary 10 - which is decimal 2

Q = binary 0010 - which is decimal 2

I can get around this by storing the length of each item as well as a bit pattern. This means that in the case of A count two events and have the pattern value 2, but for Q I count four events and have the pattern value 2. When I want to decide what a particular character is I simply look for the matching pattern and length pair.

On the right you can see my look up tables. I have arranged them so that they are easy to makes sense of. The idea is that when we find the matching pattern pairs, its offset in the array will identify the character, for example the pattern for F is at location 5 in the array, as is the characteristic length. This is another example of an array used as a look up table.

The function decode_character will look through the arrays until it finds both matching length and pattern. At this point the for loop is stopped. We can then use the value of i which the loop stopped at to identify the character which this pattern represents. Note that if no character is recognized the for loop will hit the limit and end with i holding the limit value. I look for this specially and return the character '*' if no match is found.