汉字转0 1点阵

来源:互联网 发布:随机出题软件破解 编辑:程序博客网 时间:2024/05/17 00:52
import java.io.*;class Test {private int[] unit = new int[32];private String yes = "1";private String no = "0";private PrintStream print = null;public void setYes(String str) {yes = str;}public void setNo(String str) {no = str;}private int negativeToPlus(byte b) {return b & 0xFF;}public void setOutputFile(String filename) throws Exception {print = new PrintStream(filename);}public void colseFileStream() {print.close();}private void readChina(char ch) { // 需要得到点阵的汉字byte[] buf = new byte[32];FileInputStream input = null;try {String string = Character.toString(ch);byte[] bt = string.getBytes("GBK"); // 获得国标码int a1 = negativeToPlus(bt[0]); // 转为无符号整数int a2 = negativeToPlus(bt[1]);int qh = a1 - 0xA0; // 得到区位码int wh = a2 - 0xA0;long offset = (94 * (qh - 1) + (wh - 1)) * 32; // 获得偏移量File file = new File("D:\\HZK16");input = new FileInputStream(file);input.skip(offset);input.read(buf, 0, 32);for (int i = 0; i < 32; i++)unit[i] = negativeToPlus(buf[i]);input.close();} catch (Exception e) {System.out.println("文件异常");e.printStackTrace();}}public void writeChina(char ch) { // 参数: yes是有点处显示 no是无点处显示int i, j, k;readChina(ch);for (j = 0; j < 16; j++) {for (i = 0; i < 2; i++)for (k = 0; k < 8; k++)if ((unit[j * 2 + i] & (0x80 >> k)) >= 1) // 取bit位值{System.out.print(yes);} else {System.out.print(no);}System.out.println();}}public void writeChinaToFile(String str) {try {int i, j, k;for (int x = 0; x < str.length(); x++) {readChina(str.charAt(x));for (j = 0; j < 16; j++) {for (i = 0; i < 2; i++)for (k = 0; k < 8; k++)if ((unit[j * 2 + i] & (0x80 >> k)) >= 1) // 取bit位值{print.print(yes+",");} else {print.print(no+",");}print.println();}}} catch (Exception e) {e.printStackTrace();}}static public void main(String[] str) throws Exception {Test t = new Test();String xp = "我";t.setOutputFile("D:\\123.txt"); // 确定输出 文件流t.writeChinaToFile(xp); // 要写的字符串t.colseFileStream(); // 关闭流}}