3
\$\begingroup\$

A related question got me thinking about a compact Braille dictionary. It certainly would be easier with Int16 or string but was going for most compact.
I don't think there is a case insensitive char so went with dual entries.

Dictionary<char, byte> braile = new Dictionary<char, byte>()
                        { { 'a', 0x20 }, { 'b', 0x30 }, { 'c', 0x24 }, { 'd', 0x26 }, { 'e', 0x22 }, { 'f', 0x34 }
                        , { 'g', 0x36 }, { 'h', 0x32 }, { 'i', 0x14 }, { 'j', 0x16 }, { 'k', 0x28 }, { 'l', 0x38 }
                        , { 'm', 0x2c }, { 'n', 0x2e }, { 'o', 0x2a }, { 'p', 0x3c }, { 'q', 0x3e }, { 'r', 0x3a }
                        , { 's', 0x1c }, { 't', 0x1e }, { 'u', 0x29 }, { 'v', 0x39 }, { 'w', 0x17 }, { 'x', 0x2d }
                        , { 'y', 0x2f }, { 'z', 0x2b }
                        , { 'A', 0x20 }, { 'B', 0x30 }, { 'C', 0x24 }, { 'D', 0x26 }, { 'E', 0x22 }, { 'F', 0x34 }
                        , { 'G', 0x36 }, { 'H', 0x32 }, { 'I', 0x14 }, { 'J', 0x16 }, { 'K', 0x28 }, { 'L', 0x38 }
                        , { 'M', 0x2c }, { 'N', 0x2e }, { 'O', 0x2a }, { 'P', 0x3c }, { 'Q', 0x3e }, { 'R', 0x3a }
                        , { 'S', 0x1c }, { 'T', 0x1e }, { 'U', 0x29 }, { 'V', 0x39 }, { 'W', 0x17 }, { 'X', 0x2d }
                        , { 'Y', 0x2f }, { 'Z', 0x2b }};
foreach (KeyValuePair<char, byte> c in braile)
{
    Debug.WriteLine("{0} {1}", c.Key, c.Value.ToBraile());
}
Debug.WriteLine("{0} {1}", 'w', braile['w'].ToBraile());
Debug.WriteLine("{0} {1}", 'B', braile['B'].ToBraile());

public static class MyExtensions
{
    public static string ToBraile(this byte b)
    {
        return Convert.ToString(b, 2).PadLeft(6, '0');
    }
}
\$\endgroup\$
19
  • 2
    \$\begingroup\$ Why not to use one array of bytes with a size of 256? \$\endgroup\$
    – pgs
    Commented May 7, 2017 at 19:02
  • 2
    \$\begingroup\$ @pgs Very good point, if not even an answer. \$\endgroup\$ Commented May 7, 2017 at 19:03
  • \$\begingroup\$ @pgs How you going to do the lookup? \$\endgroup\$
    – paparazzo
    Commented May 7, 2017 at 19:03
  • 1
    \$\begingroup\$ @Paparazzi how about by braille[(int)a]? \$\endgroup\$
    – t3chb0t
    Commented May 7, 2017 at 19:04
  • 1
    \$\begingroup\$ var braille = new byte[256]; braille['a'] = 0x20; and so on.. \$\endgroup\$
    – t3chb0t
    Commented May 7, 2017 at 19:19

1 Answer 1

2
\$\begingroup\$

Just turning some of the comments into an answer, more specific I'm going to address these issues in your code:

  • No need to double the lookup table – By using char.ToLower(c) you could half the size of the lookup table.
  • Shift the offset by (char) 'a' – If you shift the offset by the value of a you get a zero-based index.
  • No need longer for the char dictionary – Since we now have a zero-based index we could simply do a lookup in a byte table. (Update: And it could be a simple array also, no need for a List<byte>)
  • Since we shifted the offset, no need to include empty entries – By shifting the offset, we don't need a sparse 256 byte array. It does however, fail miserably like your code if trying to convert non-alpha characters.
  • Wouldn't it be nice to convert longer texts also? – Wouldn't you need to convert longer texts, and not only single characters? For good measure, I've added a ToString(string) to the code below.

This leads to the following code (extending on the char value):

using System.Text;

public static class MyExtensions
{
    static byte[] braille = new byte[]
      { 0x20, 0x30, 0x24, 0x26, 0x22, 0x34, 0x36, 0x32, 0x14, 0x16,
        0x28, 0x38, 0x2c, 0x2e, 0x2a, 0x3c, 0x3e, 0x3a, 0x1c, 0x1e,
        0x29, 0x39, 0x17, 0x2d, 0x2f, 0x2b
      };        

    public static string ToBraille(this char c) {
      return Convert.ToString(braille[ (byte) char.ToLower(c) - (byte) 'a'], 2).PadLeft(6, '0');
    }

    public static string ToBraille(this string s) {
      StringBuilder result = new StringBuilder();
      foreach (char c in s) {
        result.Append(c.ToBraille()).Append(" ");
      }

      return result.ToString();
    }
}
\$\endgroup\$
3
  • \$\begingroup\$ @Paparazzi the advantage of this offset method is that it counters for potentially different placing of the a which could be a problem earlier on. It also clearly shows what is happening instead of a magic number like 65. \$\endgroup\$
    – holroy
    Commented May 7, 2017 at 22:45
  • 1
    \$\begingroup\$ @t3chb0t, I've now updated it to use a simple array, and not a List<byte>. \$\endgroup\$
    – holroy
    Commented May 8, 2017 at 8:18
  • 1
    \$\begingroup\$ ToBraille(this string s) this extension could be a one-liner with s.Aggregate ;-] \$\endgroup\$
    – t3chb0t
    Commented May 8, 2017 at 10:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.