繁简体(GBBig5)字符串转化的JAVA方式实现

  http://www.zeali.net/entry/19

本文提供一个java实现中文字符繁简体互换的zip包以及主要的源代码实现说明。

繁简体(GB<=>Big5)中文字符的转化实现原理很简单,就是根据两种码表的编码规则,创建两者之间的字符对应关系表,通过程序读取这个映射表来自动查出另一种编码方式下对应字符的字节编码,从而进行逐字节的内容替换。

主功能实现的GB2Big5.java源代码如下: 已经加入里系统支持的一个类库的源代码,这个是完整版。

  1. package net.java2000.tools;
  2. import java.io.BufferedOutputStream;
  3. import java.io.FileOutputStream;
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. /**
  7.  * 用来处理GB2312/BIG5码字符互相转换的类.<br>
  8.  * 需要两个码表文件: gb-big5.table,/zeal/util/big5-gb.table.<br>
  9.  * 这两个码表可以根据具体情况补充映射不正确的码.
  10.  */
  11. public class GB2Big5 {
  12.   private static GB2Big5 pInstance = null;
  13.   private String s_big5TableFile = null;
  14.   private String s_gbTableFile = null;
  15.   private byte[] b_big5Table = null;
  16.   private byte[] b_gbTable = null;
  17.   /** 指定两个码表文件来进行初始化 */
  18.   private GB2Big5(String sgbTableFile, String sbig5TableFile) throws NullPointerException {
  19.     s_big5TableFile = sbig5TableFile;
  20.     s_gbTableFile = sgbTableFile;
  21.     if (null == b_gbTable) {
  22.       b_gbTable = getBytesFromFile(sgbTableFile);
  23.     }
  24.     if (null == b_big5Table) {
  25.       b_big5Table = getBytesFromFile(sbig5TableFile);
  26.     }
  27.     if (null == b_gbTable) {
  28.       throw new NullPointerException("No gb table can be load");
  29.     }
  30.     if (null == b_big5Table) {
  31.       throw new NullPointerException("No big5 table can be load");
  32.     }
  33.   }
  34.   public static synchronized GB2Big5 getInstance() {
  35.     // return getInstance("d://gb-big5.table","d://big5-gb.table");
  36.     return getInstance("/net/java2000/tools/gb-big5.table""/net/java2000/tools/big5-gb.table");
  37.   }
  38.   public static synchronized GB2Big5 getInstance(String sgbTableFile, String sbig5TableFile) {
  39.     if (null == pInstance) {
  40.       try {
  41.         pInstance = new GB2Big5(sgbTableFile, sbig5TableFile);
  42.       } catch (Exception e) {
  43.         System.err.println(e.toString());
  44.         pInstance = null;
  45.       }
  46.     }
  47.     return pInstance;
  48.   }
  49.   /**
  50.    * 把gbChar对应的big5字符替换掉,用来更新码表文件. 一般当发现字符映射不正确的时候可以通过这个方法来校正.
  51.    */
  52.   protected synchronized void resetBig5Char(String gbChar, String big5Char) throws Exception {
  53.     byte[] Text = new String(gbChar.getBytes(), "GBK").getBytes("GBK");
  54.     byte[] TextBig5 = new String(big5Char.getBytes(), "BIG5").getBytes("BIG5");
  55.     int max = Text.length - 1;
  56.     int h = 0;
  57.     int l = 0;
  58.     int p = 0;
  59.     int b = 256;
  60.     for (int i = 0; i < max; i++) {
  61.       h = (int) (Text[i]);
  62.       if (h < 0) {
  63.         h = b + h;
  64.         l = (int) (Text[i + 1]);
  65.         if (l < 0) {
  66.           l = b + (int) (Text[i + 1]);
  67.         }
  68.         if (h == 161 && l == 64) {
  69.           ; // do nothing
  70.         } else {
  71.           p = (h - 160) * 510 + (l - 1) * 2;
  72.           b_gbTable[p] = TextBig5[i];
  73.           b_gbTable[p + 1] = TextBig5[i + 1];
  74.         }
  75.         i++;
  76.       }
  77.     }
  78.     BufferedOutputStream pWriter = new BufferedOutputStream(new FileOutputStream(s_gbTableFile));
  79.     pWriter.write(b_gbTable, 0, b_gbTable.length);
  80.     pWriter.close();
  81.   }
  82.   /**
  83.    * 把big5Char对应的gb字符替换掉,用来更新码表文件. 一般当发现字符映射不正确的时候可以通过这个方法来校正.
  84.    */
  85.   protected synchronized void resetGbChar(String big5Char, String gbChar) throws Exception {
  86.     byte[] TextGb = new String(gbChar.getBytes(), "GBK").getBytes("GBK");
  87.     byte[] Text = new String(big5Char.getBytes(), "BIG5").getBytes("BIG5");
  88.     int max = Text.length - 1;
  89.     int h = 0;
  90.     int l = 0;
  91.     int p = 0;
  92.     int b = 256;
  93.     for (int i = 0; i < max; i++) {
  94.       h = (int) (Text[i]);
  95.       if (h < 0) {
  96.         h = b + h;
  97.         l = (int) (Text[i + 1]);
  98.         if (l < 0) {
  99.           l = b + (int) (Text[i + 1]);
  100.         }
  101.         if (h == 161 && l == 64) {
  102.           ; // do nothing
  103.         } else {
  104.           p = (h - 160) * 510 + (l - 1) * 2;
  105.           b_big5Table[p] = TextGb[i];
  106.           b_big5Table[p + 1] = TextGb[i + 1];
  107.         }
  108.         i++;
  109.       }
  110.     }
  111.     BufferedOutputStream pWriter = new BufferedOutputStream(new FileOutputStream(s_big5TableFile));
  112.     pWriter.write(b_big5Table, 0, b_big5Table.length);
  113.     pWriter.close();
  114.   }
  115.   /** 把gb2312编码的字符串转化成big5码的字节流 */
  116.   public byte[] gb2big5(String inStr) throws Exception {
  117.     if (null == inStr || inStr.length() <= 0) {
  118.       return "".getBytes();
  119.       // return "";
  120.     }
  121.     byte[] Text = new String(inStr.getBytes(), "GBK").getBytes("GBK");
  122.     int max = Text.length - 1;
  123.     int h = 0;
  124.     int l = 0;
  125.     int p = 0;
  126.     int b = 256;
  127.     byte[] big = new byte[2];
  128.     for (int i = 0; i < max; i++) {
  129.       h = (int) (Text[i]);
  130.       if (h < 0) {
  131.         h = b + h;
  132.         l = (int) (Text[i + 1]);
  133.         if (l < 0) {
  134.           l = b + (int) (Text[i + 1]);
  135.         }
  136.         if (h == 161 && l == 64) {
  137.           big[0] = big[1] = (byte) (161 - b);
  138.         } else {
  139.           p = (h - 160) * 510 + (l - 1) * 2;
  140.           try {
  141.             big[0] = (byte) (b_gbTable[p] - b);
  142.           } catch (Exception e) {
  143.             big[0] = 45;
  144.           }
  145.           try {
  146.             big[1] = (byte) (b_gbTable[p + 1] - b);
  147.           } catch (Exception e) {
  148.             big[1] = 45;
  149.           }
  150.         }
  151.         Text[i] = big[0];
  152.         Text[i + 1] = big[1];
  153.         i++;
  154.       }
  155.     }
  156.     return Text;
  157.     // return new String(Text);
  158.   }
  159.   /** 把big5码的字符串转化成gb2312码的字符串 */
  160.   public String big52gb(String inStr) throws Exception {
  161.     if (null == inStr || inStr.length() <= 0) {
  162.       return "";
  163.     }
  164.     byte[] Text = new String(inStr.getBytes(), "BIG5").getBytes("BIG5");
  165.     int max = Text.length - 1;
  166.     int h = 0;
  167.     int l = 0;
  168.     int p = 0;
  169.     int b = 256;
  170.     byte[] big = new byte[2];
  171.     for (int i = 0; i < max; i++) {
  172.       h = (int) (Text[i]);
  173.       if (h < 0) {
  174.         h = b + h;
  175.         l = (int) (Text[i + 1]);
  176.         if (l < 0) {
  177.           l = b + (int) (Text[i + 1]);
  178.         }
  179.         if (h == 161 && l == 161) {
  180.           big[0] = (byte) (161 - b);
  181.           big[1] = (byte) (64 - b);
  182.         } else {
  183.           p = (h - 160) * 510 + (l - 1) * 2;
  184.           try {
  185.             big[0] = (byte) (b_big5Table[p] - b);
  186.           } catch (Exception e) {
  187.             big[0] = 45;
  188.           }
  189.           try {
  190.             big[1] = (byte) (b_big5Table[p + 1] - b);
  191.           } catch (Exception e) {
  192.             big[1] = 45;
  193.           }
  194.         }
  195.         Text[i] = big[0];
  196.         Text[i + 1] = big[1];
  197.         i++;
  198.       }
  199.     }
  200.     return new String(Text);
  201.   }
  202.   /** 把文件读入字节数组,读取失败则返回null */
  203.   private static byte[] getBytesFromFile(String inFileName) {
  204.     try {
  205.       InputStream in = GB2Big5.class.getResourceAsStream(inFileName);
  206.       byte[] sContent = StreamConverter.toByteArray(in);
  207.       in.close();
  208.       return sContent;
  209.       /*
  210.        * java.io.RandomAccessFile inStream = new java.io.RandomAccessFile(inFileName,"r"); byte[] sContent = new byte[ (int)
  211.        * (inStream.length())]; inStream.read(sContent); inStream.close(); return sContent;
  212.        */
  213.     } catch (Exception e) {
  214.       e.printStackTrace();
  215.       return null;
  216.     }
  217.   }
  218.   public static void main(String[] args) throws Exception {
  219.     if (args.length < 2) {
  220.       System.out.println("Usage: net.java2000.tools.GB2Big5 [-gb | -big5] inputstring");
  221.       System.exit(1);
  222.       return;
  223.     }
  224.     boolean bIsGB = true;
  225.     String inStr = "";
  226.     for (int i = 0; i < args.length; i++) {
  227.       if (args[i].equalsIgnoreCase("-gb")) {
  228.         bIsGB = true;
  229.       } else if (args[i].equalsIgnoreCase("-big5")) {
  230.         bIsGB = false;
  231.       } else {
  232.         inStr = args[i];
  233.       }
  234.     }
  235.     GB2Big5 pTmp = GB2Big5.getInstance();
  236.     String outStr = "";
  237.     if (bIsGB) {
  238.       outStr = pTmp.big52gb(inStr);
  239.     } else {
  240.       outStr = new String(pTmp.gb2big5(inStr), "BIG5");
  241.     }
  242.     System.out.println("String [" + inStr + "] converted into:/n[" + outStr + "]");
  243.   }
  244. }
  245. class StreamConverter {
  246.   public StreamConverter() {
  247.   }
  248.   public static byte[] toByteArray(InputStream input) throws IOException {
  249.     int status = 0;
  250.     int totalBytesRead = 0;
  251.     int blockCount = 1;
  252.     byte dynamicBuffer[] = new byte[5000 * blockCount];
  253.     byte buffer[] = new byte[5000];
  254.     boolean endOfStream = false;
  255.     do {
  256.       if (endOfStream)
  257.         break;
  258.       int bytesRead = 0;
  259.       if (input.available() != 0) {
  260.         status = input.read(buffer);
  261.         endOfStream = status == -1;
  262.         if (!endOfStream)
  263.           bytesRead = status;
  264.       } else {
  265.         status = input.read();
  266.         endOfStream = status == -1;
  267.         buffer[0] = (byte) status;
  268.         if (!endOfStream)
  269.           bytesRead = 1;
  270.       }
  271.       if (!endOfStream) {
  272.         if (totalBytesRead + bytesRead > 5000 * blockCount) {
  273.           blockCount++;
  274.           byte newBuffer[] = new byte[5000 * blockCount];
  275.           System.arraycopy(dynamicBuffer, 0, newBuffer, 0, totalBytesRead);
  276.           dynamicBuffer = newBuffer;
  277.         }
  278.         System.arraycopy(buffer, 0, dynamicBuffer, totalBytesRead, bytesRead);
  279.         totalBytesRead += bytesRead;
  280.       }
  281.     } while (true);
  282.     byte result[] = new byte[totalBytesRead];
  283.     if (totalBytesRead != 0)
  284.       System.arraycopy(dynamicBuffer, 0, result, 0, totalBytesRead);
  285.     return result;
  286.   }
  287. }

由于这里不能上传附件,所需要的2个字体对照表,请到这里下载,或者到原作者的网页下载。那里还有完整的项目包

http://www.java2000.net/p8902















<script type="text/javascript"> </script> <script type="text/javascript" src="http://pagead2.googlesyndication.com/pagead/show_ads.js"> </script>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值