【问题标题】:Converting a String to data from a lookup table将字符串转换为查找表中的数据
【发布时间】:2011-11-07 22:23:52
【问题描述】:

我有一个我编写的程序,它有 26 个表示字母的 int 数组。它们每个都包含 5 个二进制数字,代表将在显示屏上点亮的灯。我需要做的是将字符串转换为二进制数据。例如,如果你看下面的代码:

int B[] = {B1111111, B1001001, B1001001, B0110110, B0000000};
int O[] = {B0111110, B1000001, B1000001, B0111110, B0000000};

所以如果字符串是“BOB”,我需要它来创建一个看起来像这样的数组:

int CurrentWord[] = {B1111111, B1001001, B1001001, B0110110, B0000000, B0111110, B1000001, B1000001, B0111110, B0000000, B1111111, B1001001, B1001001, B0110110, B0000000};

我可以看到也许用一堆开关来做到这一点,但一定有更好的方法。

PS,我知道我的代码在目标 c 中,我希望在 C# 中执行此操作

【问题讨论】:

  • 可能有很多方法 - 我能想到的一种方法是遍历字符串的字符,将每个字符转换为 int,然后从 1 开始,使用按位或,这会给你布尔结果如果设置了低位......然后左移,然后重复......
  • 您想要使用 C# 还是 Objective-c 的解决方案?从您的问题和您使用的标签中不清楚
  • 很抱歉,我需要 Objective-c 中的代码,我的应用程序有一个 C# 程序,它与运行 Objective-c 程序的微控制器通信。字符串被发送到微控制器,它必须进行转换。
  • 正如您可能已经通过 C# 答案的数量猜到了,您应该更加小心如何标记您的问题。

标签: c# iphone arrays string


【解决方案1】:

这是一个数组数组的工作。

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

要进行查找,get the ASCII value of the upper-case character (which will be from 64 to 90) and subtract 64,并将其用作您的数组索引:

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"

显然,要完成此操作,您需要遍历所有字符并将每个结果复制到您的输出中。

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。

更新:C#

这在 C# 中要容易得多。概念保持不变,但代码更简洁。

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

获取结果的方法如下:

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

需要注意的一些很酷的事情:C# 允许您迭代字符串,就像它是一个 char 数组一样,它允许您执行 char 数学运算,因此代码非常简单。耶!

【讨论】:

  • 好的,这一切都非常有帮助,但是如果我的字符包含多个字符,如何获取每个字母的 ASCII 码。例如,如果我的 char = "Tanner"
  • 看看这个答案:stackoverflow.com/questions/7141850/…(我用一个例子更新了我的答案)
  • 谢谢!我实际上已经将项目的这一部分移到了 C# 模块中。你能给我一些更新,因为我现在正在使用 C#,显然对于初学者来说 NSString 没有被使用......再次感谢。
  • 使用 C# 使这种方式更容易......除了创建 MAP,它基本上是 1 行代码!我相应地更新了我的答案。
【解决方案2】:

我认为这可行:

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

结果

[01000010][01001111][01000010]

【讨论】:

    猜你喜欢
    • 2015-10-29
    • 1970-01-01
    • 2020-09-21
    • 2021-11-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-06-16
    相关资源
    最近更新 更多