萍聚社区-德国热线-德国实用信息网

 找回密码
 注册

微信登录

微信扫一扫,快速登录

萍聚头条

查看: 684|回复: 0

1-1-16-2-2 Unicode 汉字与GB内码的转换

[复制链接]
发表于 2003-5-11 22:31 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?注册 微信登录

x
发信人: intranetworm (小虫), 信区: Java
标  题: Unicode 汉字与GB内码的转换
发信站: BBS 水木清华站 (Wed Aug 27 13:44:45 1997)

这是我编的一个转换程序,使用时现将前面的码表存成文件,例如table.txt
创建一个GBUnicode实例,new GBUnicode("table.txt")
以后调用GB2Uni和Uni2GB作内码转换.注意GB内码是用两个字节表示的.

  1. import java.io.*;
  2. import java.util.Hashtable;

  3. class GBUnicode{
  4.         byte high[]=new byte[6763],low[]=new byte[6763];
  5.         char unichar[]=new char[6763];
  6.         Hashtable UniGB;

  7.         public GBUnicode(String table_file)throws IOException
  8.         {
  9.                 //BufferedInputStream tables=new BufferedInputStream (new FileIn
  10.                 DataInputStream tables=new DataInputStream (new FileInputStream(
  11.                 int i,n=0;
  12.                 byte b,bl,bh,num[]=new byte[20];

  13.                 UniGB=new Hashtable(7000,1);
  14.                 while (n<6763 ){
  15.                         do{
  16.                                 bh=(byte)tables.read();
  17.                         }while ((char)bh<=' '); //find first non-blank char
  18.                         bl=(byte)tables.read();
  19.                         high[n]=bh;
  20.                         low[n]=bl;
  21.                         do{
  22.                                 b=(byte)tables.read();
  23.                         }while (b!=(byte)':'); //find ':'
  24.                         do{
  25.                                 b=(byte)tables.read();
  26.                         }while ((char)b<=' '); //find next non-blank char to rea
  27.                         i=0;
  28.                         while ((char)b>='0' && (char)b<='9'){
  29.                                 num[i++]=b;
  30.                                 b=(byte)tables.read();
  31.                         }
  32.                         unichar[n]=(char)Integer.parseInt(new String(num,0,0,i))
  33.                         if (UniGB.get(new Character(unichar[n]))!= null)
  34.                                 System.out.println("Duplicated : "+unichar[n]);
  35.                         UniGB.put(new Character(unichar[n]),new Integer(n));
  36.                         n=n+1;
  37.                 }
  38.                 tables.close();
  39.         }


  40.         private int getGBindex(byte high,byte low){
  41.                 int i,j;
  42.                 i=high-(byte)0xb0;
  43.                 j=low-(byte)0xa1;
  44.                 if (i <39) {// L1 Chinese
  45.                         if (j<0 || j>94)
  46.                                 return -1;
  47.                         return (i*94+j);
  48.                 }
  49.                 else if (i==39) {//one of the last 89 L1 Chinese
  50.                         if (j<0 || j>89)
  51.                                 return -1;
  52.                         return (i*94+j);
  53.                 }
  54.                 else {//L2 Chinese
  55.                         if (j<0 || j>94)
  56.                                 return -1;
  57.                         return (i*94+j-5);
  58.                 }
  59.         }

  60.         public byte[] Uni2GB(char unicode) {

  61.                 Integer index=(Integer)UniGB.get(new Character(unicode));
  62.                 if (index==null)
  63.                         return null;
  64.                 byte ch[]=new byte[2];
  65.                 ch[0]=high[index.intValue()];
  66.                 ch[1]=low[index.intValue()];
  67.                 return ch;

  68.         public byte[] Uni2GB(char unicode) {

  69.                 Integer index=(Integer)UniGB.get(new Character(unicode));
  70.                 if (index==null)
  71.                         return null;
  72.                 byte ch[]=new byte[2];
  73.                 ch[0]=high[index.intValue()];
  74.                 ch[1]=low[index.intValue()];
  75.                 return ch;
  76.         }

  77.         public char GB2Uni(byte high, byte low) {
  78.                 int index=getGBindex(high,low);
  79.                 if (index ==-1) //not GB Chinese
  80.                         return 0;
  81.                 return(unichar[index]);
  82.         }
  83. }
复制代码

--
※ 来源:·BBS 水木清华站 bbs.net.tsinghua.edu.cn·[FROM: organ.ncic.ac.c]
Die von den Nutzern eingestellten Information und Meinungen sind nicht eigene Informationen und Meinungen der DOLC GmbH.
您需要登录后才可以回帖 登录 | 注册 微信登录

本版积分规则

手机版|Archiver|AGB|Impressum|Datenschutzerklärung|萍聚社区-德国热线-德国实用信息网

GMT+1, 2025-2-12 08:48 , Processed in 0.059954 second(s), 20 queries , MemCached On.

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表