747
技術社區[雲棲]
漢字轉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