Search code examples
cbitmapchar

Is it possible to get ASCII font bitmap array just from a binary number?


I'm looking for a solution where I can generate a 6*8 bitmap just from a binary number.

Assume that you were given the char A which is binary 0b01000001. Is it possible for you to create a 6*8 bitmap (two colors only 0 and 1) from the letter A in C?

Or do I need to have a character set list such as this to find how the bitmap should be shaped?

enter image description here


Solution

  • Of course you have to form a character map for each character.

    Yet code can reflect your font design, tedious, but kinda fun.

    #define MAP(s) ((s[0]=='*')<<5 | (s[1]=='*')<<4 | (s[2]=='*')<<3 | \
                    (s[3]=='*')<<2 | (s[4]=='*')<<1 | (s[5]=='*')<<0)
    static const unsigned char map[256][8] = {
      // ...
      // 2 examples
      // A
      { MAP( "  *** "),
        MAP( " *   *"),
        MAP( " *   *"),
        MAP( " *****"),
        MAP( " *   *"),
        MAP( " *   *"),
        MAP( " *   *"),
        MAP( "      ") },
        // B
      { MAP( " **** "),
        MAP( " *   *"),
        MAP( " *   *"),
        MAP( " **** "),
        MAP( " *   *"),
        MAP( " *   *"),
        MAP( " **** "),
        MAP( "      ") },
        // ...
    };
    

    Various macros can wrap the initialization in an alternate manner as needed.