阅读747 返回首页    go 阿里云 go 技术社区[云栖]


汉字转0 1点阵

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(); // 关闭流

	}
}

最后更新:2017-04-03 16:48:59

  上一篇:go 耿建玲关于SQL视频的那点事
  下一篇:go 利用ffmpeg做视频解码的顺序