1

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#

BenMorel
  • 34,448
  • 50
  • 182
  • 322
Tanner Ewing
  • 337
  • 4
  • 18
  • there are probably lots of ways - one i can think of is looping through the chars of the string, casting each to int, then start with 1, use bit-wise OR, that gives you boolean result if low bit is set.. then do left shift, and repeat... – Aaron Anodide Nov 07 '11 at 22:31
  • 1
    Did you want a solution using C# or objective-c? Not clear from your question and the tags you use – BrokenGlass Nov 07 '11 at 22:33
  • I am sorry for that, I need the code in objective-c, my application has a C# program that communicates with a microcontroller that runs a objective-c program. The string is sent to the microcontroller and it must do the conversion. – Tanner Ewing Nov 07 '11 at 22:43
  • 1
    As you might have guessed by the number of C# answers, you should be more careful how you tag your questions. – Scott Rippey Nov 08 '11 at 22:46

2 Answers2

1

This is a job for an array of arrays.

Objective C

int[][] map = new int[26][];
map[0] = {B0000000, B0000000, B0000000, B0000000, B0000000}; // Letter "A"
map[1] = {B1111111, B1001001, B1001001, B0110110, B0000000}; // Letter "B"
... Populate the array ...

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:

char c = 'B';                   // char can be treated as an int
int index = toupper(c) - 'A';   // See the link above for an explanation
int[] result = map[ascii];      // Returns the map for "B"

Obviously, to finish this off, you'd need to loop over all chars and copy each result to your output.

NSString *myString = [NSString stringWithString:@"Tanner"];
unichar c;
for(int i=0; i<[myString length]; i++) {
    c = [myString characterAtIndex:i];
                                    // char can be treated as an int
    int index = toupper(c) - 'A';   // See the link above for an explanation
    int[] result = map[ascii];      // Returns the map for "B"
    
    ... Append the result to a list of results ...
}

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.

public static class Lights
{
    public static byte[] Encode(string input)
    {
        // Convert to ASCII values, get the map, and flatten it:
        return input.ToUpper().SelectMany(c => map[c-65]).ToArray();
    }
    
    // Note: C# does not have Binary Literals, so here's an alternative:
    private const byte B0000000 = 0, B0000001 = 1, B0000010 = 2, B0000011 = 3, B0000100 = 4, /* ETC */ B1111111 = 127, B1001001 = 73, B0110110 = 102, B0111110 = 126, B1000001 = 129;
        
    // Create the map:
    private static byte[][] map = new []{
                    /* A */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* B */ new[]{ B1111111, B1001001, B1001001, B0110110, B0000000 },
                    /* C */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* D */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* E */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* F */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* G */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* H */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* I */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* J */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* K */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* L */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* M */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* N */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* O */ new[]{ B0111110, B1000001, B1000001, B0111110, B0000000 },
                    /* P */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* Q */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* R */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* S */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* T */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* U */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* V */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* W */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* X */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* Y */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                    /* Z */ new[]{ B0000000, B0000000, B0000000, B0000000, B0000000 },
                };
}

Here's how to get the results:

byte[] lights = Lights.Encode("BOB");

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!

Community
  • 1
  • 1
Scott Rippey
  • 15,614
  • 5
  • 70
  • 85
  • ok, this is all very helpful, but how to I get the ASCII codes for each individual letter if my char contains more than one. For example if my char = "Tanner" – Tanner Ewing Nov 08 '11 at 21:27
  • Take a look at this answer: http://stackoverflow.com/questions/7141850/how-to-iterate-through-a-strings-characters (I updated my answer with an example) – Scott Rippey Nov 08 '11 at 22:38
  • Thanks for this!! I have actually moved this part of the project into a C# module. Can you give me some updates since I am now using C#, obviously for starters NSString isn't used... Thanks again. – Tanner Ewing Nov 29 '11 at 19:42
  • Using C# makes this WAY easier ... apart from creating the MAP, it's basically 1 line of code! I updated my answer accordingly. – Scott Rippey Dec 01 '11 at 00:31
0

I think this works:

void Main() {
    var s = ToBinary("BOB");
}
string ToBinary(string s) {
    var r = "";
    foreach (var c in s.ToCharArray()) {
        string w = "";
        for (int i = 1; i < 257; i = i << 1)
            w = ((c & i) > 0 ? "1" : "0") + w;
        r += "[" + w + "]";
    }
    return r;
}

result

[01000010][01001111][01000010]
Aaron Anodide
  • 16,906
  • 15
  • 62
  • 121