我有一个我编写的程序,它有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#
来做答案 0 :(得分:1)
这是一组数组的工作。
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#中更容易。概念保持不变,但代码更整洁。
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数学,所以代码非常简单。耶!
答案 1 :(得分:0)
我认为这有效:
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]