从查找表将字符串转换成数据 [英] Converting a String to data from a lookup table

查看:118
本文介绍了从查找表将字符串转换成数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有我写了有26 INT阵列重新presenting的英文字母的程序。他们每个人都包含重新将显示屏上亮起present灯5二进制数。我需要做的是将字符串转换成二进制数据。例如,如果你看一下下面的code:

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};

因此​​,如果字符串是BOB我需要它来创建一个数组,看起来是这样的:

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,我知道我的code是目标C,我要做到这在C#

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.

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 ...

要做好查找,得到大写字符的ASCII值(将是从64到90),并减去64 ,并使用它作为您的数组索引:

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 ...
}

请原谅任何Objective-C的语法问题,问题是标签的C#,所以我不得不去适应的Objective-C。

Please excuse any Objective-C syntax issues, the question is tagged C#, so I had to adapt to Objective-C.

这是在C#的方式更容易。这个概念是一样的,但code是更整洁。

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");

一对夫妇很酷的事情需要注意:C#用于遍历字符串,如果它是一个字符数组,它可以让你执行焦炭数学,所以code是超级简单。耶!

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!

这篇关于从查找表将字符串转换成数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆