lianxi/Word.java

278 lines
6.2 KiB
Java
Raw Normal View History

2024-12-15 18:25:59 +08:00
import java.io.File;
import java.io.FileInputStream;
import java.io.PrintStream;
public class Word {
public final static String ZK_PATH = "/HZK16";
public final static String ENCODE = "GB2312";
private int[] unit = new int[32];
private String yes = "$";// "<22><>";
private String no = " ";
private PrintStream print = null;
public Word() {
// String str = "<22><>";
//
// try {
//
// byte[] b = str.getBytes("GB2312");
//
// for (int i = 0; i < b.length; i++) {
//
// System.out.println(Integer.toHexString(b[i]));
//
// }
//
// } catch (Exception ex) {
//
// }
}
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) { // <20><>Ҫ<EFBFBD>õ<EFBFBD><C3B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ĺ<EFBFBD><C4BA><EFBFBD>
byte[] buf = new byte[32];
FileInputStream input = null;
try {
if (ch < 0x80) {
long offset = (ch + 155) * 32; // <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
File file = new File("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();
}
if (ch > 0x80) {
String string = Character.toString(ch);
byte[] bt = string.getBytes("GBK"); // <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
int a1 = negativeToPlus(bt[0]); // תΪ<D7AA>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
int a2 = negativeToPlus(bt[1]);
int qh = a1 - 0xA0; // <20>õ<EFBFBD><C3B5><EFBFBD>λ<EFBFBD><CEBB>
int wh = a2 - 0xA0;
long offset = (94 * (qh - 1) + (wh - 1)) * 32; // <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
File file = new File("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();
}
// String string = Character.toString(ch);
// byte[] bt = string.getBytes("GBK"); // <20><><EFBFBD>ù<EFBFBD><C3B9><EFBFBD><EFBFBD><EFBFBD>
// int a1 = negativeToPlus(bt[0]); // תΪ<D7AA>޷<EFBFBD><DEB7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// int a2 = negativeToPlus(bt[1]);
// int qh = a1 - 0xA0; // <20>õ<EFBFBD><C3B5><EFBFBD>λ<EFBFBD><CEBB>
// int wh = a2 - 0xA0;
// long offset = (94 * (qh - 1) + (wh - 1)) * 32; // <20><><EFBFBD><EFBFBD>ƫ<EFBFBD><C6AB><EFBFBD><EFBFBD>
//
// File file = new File("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("<EFBFBD>ļ<EFBFBD><EFBFBD>");
e.printStackTrace();
}
}
public void writeChina(char ch) { // <20><><EFBFBD><EFBFBD>: yes<65><73><EFBFBD>е<D0B5><E3B4A6>ʾ no<6E><6F><EFBFBD>޵㴦<DEB5><E3B4A6>ʾ
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_V(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();
}
}
public void writeChinaToFile_H(String str) {
try {
int i, j, k;
{
for (j = 0; j < 16; j++) {
print.print(" ");
// <20><><EFBFBD><EFBFBD>ÿ<EFBFBD><C3BF><EFBFBD>ֵ<EFBFBD>ͬһ<CDAC><D2BB>///////////////
for (int x = 0; x < str.length(); x++) {
readChina(str.charAt(x));
for (i = 0; i < 2; i++) {
for (k = 0; k < 8; k++) {
if ((unit[j * 2 + i] & (0x80 >> k)) >= 1) // ȡbitλֵ
{
print.print(yes);
2024-12-15 18:34:57 +08:00
System.out.print(yes);
2024-12-15 18:25:59 +08:00
} else {
print.print(no);
2024-12-15 18:34:57 +08:00
System.out.print(no);
2024-12-15 18:25:59 +08:00
}
}
}
print.print(" ");
2024-12-15 18:34:57 +08:00
System.out.print(" ");
2024-12-15 18:25:59 +08:00
}
print.println();
2024-12-15 18:34:57 +08:00
System.out.println();
2024-12-15 18:25:59 +08:00
// //////////////////////////////////
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void main(String[] args) throws Exception {
Word charcode = new Word();
String str = "ALIPAY<EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
String str2 = "֧<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>";
charcode.setOutputFile("output.txt"); // ȷ<><C8B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20>ļ<EFBFBD><C4BC><EFBFBD>
charcode.writeChinaToFile_H(str);
charcode.writeChinaToFile_H(str2); // Ҫд<D2AA><D0B4><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD>
charcode.colseFileStream(); // <20>ر<EFBFBD><D8B1><EFBFBD>
// int code[] = null;
// byte data[] = null;
// Word getcode = new Word();
// code = getcode.getByteCode("<22><>");
// data = getcode.read(code[0], code[1]);
// for (int i = 0; i < 32; i++) {
// System.out.println(data[i]);
// }
}
}
// public int[] getByteCode(String str) {
//
// int[] byteCode = new int[2];
//
// try {
//
// byte[] data = str.getBytes(ENCODE);
//
// byteCode[0] = data[0] < 0 ? 256 + data[0] : data[0];
//
// byteCode[1] = data[1] < 0 ? 256 + data[1] : data[1];
//
// } catch (Exception ex) {
//
// }
//
// return byteCode;
//
// }
//
// public byte[] read(int areaCode, int posCode) {
//
// byte[] data = null;
// try {
// int area = areaCode - 0xa0;// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵ<EFBFBD><CAB5><EFBFBD><EFBFBD>
// int pos = posCode - 0xa0;// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʵλ<CAB5><CEBB>
// InputStream in = getClass().getResourceAsStream(ZK_PATH);
// long offset = 32 * ((area - 1) * 94 + pos - 1);
// in.skip(offset);
// data = new byte[32];
// in.read(data, 0, 32);
// in.close();
// } catch (Exception ex) {
//
// }
//
// return data;
//
// }
// protected void drawString(Graphics g, String str, int x, int y, int
// color) {
//
// byte[] data = null;
// int[] code = null;
// int byteCount;// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ݵĵڼ<C4B5><DABC><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
// int lCount;// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// g.setColor(color);
// for (int i = 0; i < str.length(); i++) {
// if (str.charAt(i) < 0x80) {// <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
// g.drawString(str.substring(i, i + 1), x + (i << 4), y, 0);
// continue;
// }
//
// code = getByteCode(str.substring(i, i + 1));
// data = read(code[0], code[1]);
// byteCount = 0;
// for (int line = 0; line < 16; line++) {
// lCount = 0;
// for (int k = 0; k < 2; k++) {
// for (int j = 0; j < 8; j++) {
// if ((data[byteCount] & mask[j]) == mask[j]) {
// g.drawLine(x + lCount + (i << 4), y + line, x
// + lCount + (i << 4), y + line);
// }
//
// lCount++;
//
// }
// byteCount++;
// }
// }
// }
// }