I have a program that I have written that has 26 int arrays representing the letters of the alphabet. They each contain 5 binary numbers that represent Lights that will be lit up on a display. What I need to do is to convert a string into the binary data. For example, If you look at the code below:
int B[] = {B1111111, B1001001, B1001001, B0110110, B0000000};
int O[] = {B0111110, B1000001, B1000001, B0111110, B0000000};
So if the string was “BOB” I need it to create an array that looks something like this:
int CurrentWord[] = {B1111111, B1001001, B1001001, B0110110, B0000000, B0111110, B1000001, B1000001, B0111110, B0000000, B1111111, B1001001, B1001001, B0110110, B0000000};
I can see maybe doing this with a bunch of switches, but there must be a better way.
PS, I know my code is in objective c, I am looking to do this in C#
This is a job for an array of arrays.
Objective C
To do the lookup, get the ASCII value of the upper-case character (which will be from 64 to 90) and subtract 64, and use that as your array index:
Obviously, to finish this off, you’d need to loop over all chars and copy each result to your output.
Please excuse any Objective-C syntax issues, the question is tagged C#, so I had to adapt to Objective-C.
Update: C#
This is way easier in C#. The concept remains the same, but the code is much neater.
Here’s how to get the results:
A couple of cool things to note: C# lets you iterate a string as if it was a char array, and it lets you perform char math, so the code is super-simple. Yay!