I'm currently stuck in my program.

I'm supposed to create a new method that inputs a message string and takes each letter and converts it into a two dimensional array of 1's and 0's. For example the letter 'H' would be

10001
10001
11111
10001
10001

My thoughts were to create a for loop and use the .charAt() method to isolate each char in the string. But from there I'm not quite sure where to go to take that char and convert it into the two dimensional array. Any suggestions?

Dani AI

Generated

— you were right to loop with charAt(); the missing piece is how each character gets mapped to a bitmap. ’s suggestion to inspect the ASCII value and binary is useful for code points, but that binary does not produce the visual 5x5 glyph you showed. Two practical approaches:

  • Create a fixed lookup table (hard-coded 5x5 bitmaps) if you need a specific pixel font for only A-Z and digits.
  • Render each glyph with Java2D into a BufferedImage and convert pixels into a 0/1 grid. This is flexible, handles any font, and can be down-sampled to exactly the grid size you want.

A compact rendering approach (draw large, then down-sample into a rows x cols matrix):

public static int[][] charToMatrix(char ch, int cols, int rows, Font font, int scale) {
    int W = cols * scale, H = rows * scale;
    BufferedImage img = new BufferedImage(W, H, BufferedImage.TYPE_INT_RGB);
    Graphics2D g = img.createGraphics();
    g.setColor(Color.WHITE); g.fillRect(0, 0, W, H);
    g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_OFF);
    g.setColor(Color.BLACK); g.setFont(font);
    FontMetrics fm = g.getFontMetrics();
    int x = (W - fm.charWidth(ch)) / 2;
    int y = (H - fm.getHeight()) / 2 + fm.getAscent();
    g.drawString(String.valueOf(ch), x, y);
    g.dispose();

    int[][] matrix = new int[rows][cols];
    for (int r = 0; r < rows; r++) {
        for (int c = 0; c < cols; c++) {
            int sum = 0;
            for (int yy = r * scale; yy < (r + 1) * scale; yy++) {
                for (int xx = c * scale; xx < (c + 1) * scale; xx++) {
                    int rgb = img.getRGB(xx, yy);
                    int red = (rgb >> 16) & 0xFF, green = (rgb >> 8) & 0xFF, blue = rgb & 0xFF;
                    sum += (int)(0.299 * red + 0.587 * green + 0.114 * blue);
                }
            }
            int avg = sum / (scale * scale);
            matrix[r][c] = avg < 128 ? 1 : 0;
        }
    }
    return matrix;
}

Tips and troubleshooting:

  • Use a monospaced font and pick scale large enough (4–10) so the glyph detail survives down-sampling.
  • Turn off antialiasing for crisper binary results, or use a threshold that fits your font.
  • Cache results per character in a Map<Character,int[][]> for performance when converting full strings.
  • For exact, handcrafted 5x5 letters (identical to the example), use a hard-coded lookup table instead of rendering.

Hi,

I know the ASCII code for the letter H is: 1001000
You can get the decimal number of H and then try to convert to binary. Here is a small program to do the conversion. You can figureout how to place this into a dimensinal array.
import java.util.*;

class ConvertString
{
public static void main (String [] args)
{
char c = 'H';
int a = c - 0;
int b = 0; intializing b to zero
Stack s = new Stack(); // declaring a new stack

System.out.println(c +" equal in decimal to: " +a);

do
{
b = (a%2); //getting the remainder
s.push(String.valueOf(b)); // push the value of b into the stack
a /= 2; // remove right most digit
} while(a != 0); //continue until all digits computed

while(!s.empty())

System.out.println(s.pop()); //result of H in binary.
}
}

Hope this helped little and good luck.
Dounia

Thanks a lot. Sorry it took me so long to get respond. I really appreciate the help. It came in handy

~kai

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.