This post might be more theory than code.
I was wondering if there is a (relatively) simple way to use a text table (basically an array of chars) and replace the chars in a string based on their value.
Let me elaborate.
Let’s say we have this two line table:
table[0x0] = new char[] {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p'};
table[0x1] = new char[] {'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', ']', ',', '/', '.', '~', '&'};
Each array has 16 members, 0-F in hex.
Say we have a string “hello” converted to hex (68 65 6C 6C 6F). I want to take these hex numbers, and map them to the new locations as defined in the table above.
So, “hello” would now look like this:
07 04 0B 0B 0E
I can easily convert the string into an array, but I am stuck on what to do next. I feel a foreach loop would do the trick, but it’s exact contents I do not yet know.
Is there an easy way to do this? It seems like it shouldn’t be too hard, but I’m not quite sure how to go about doing it.
Thank you very much for any help at all!
Output:
Note that if you provide an input character that isn’t in the lookup table, you’re not going to notice it right away!
Selectreturns anIEnumerable, that is lazily-evaluated only when you go to use it. At that point, if the input character is not found, the dictionary[]call will throw an exeception.One way to make this more obvious is to call
ToArray()after the Select, so you have an array of indices, and not anIEnumerable. This will force the evaluation to happen immediately:Reference:
Array.IndexOfEnumerable.ToDictionaryEnumerable.SelectString.Join