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

	}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值