zoukankan      html  css  js  c++  java
  • 和荣笔记 从 GB2312 到 Unicode 转换表制作程式


    这本手册里的字符与汉字编码列表由下面的程式所生成。

      1/**
      2* GB2312Unicde.java
      3* Copyright (c) 1997-2003 by Dr. Herong Yang
      4*/

      5import java.io.*;
      6import java.nio.*;
      7import java.nio.charset.*;
      8class GB2312Unicde {
      9static OutputStream out = null;
     10static char hexDigit[] = {'0''1''2''3''4''5''6''7',
     11'8''9''A''B''C''D''E''F'}
    ;
     12static int b_out[] = {201,267,279,293,484,587,625,657,734,782,827,
     13874,901,980,5590}
    ;
     14static int e_out[] = {216,268,280,294,494,594,632,694,748,794,836,
     15894,903,994,5594}
    ;
     16public static void main(String[] args) {
     17try {
     18out = new FileOutputStream("gb2312.gb");
     19writeCode();
     20out.close();
     21}
     catch (IOException e) {
     22System.out.println(e.toString());
     23}

     24}

     25public static void writeCode() throws IOException {
     26boolean reserved = false;
     27String name = null;
     28// GB2312 is not supported by JDK. So I am using GBK.
     29CharsetDecoder gbdc = Charset.forName("GBK").newDecoder();
     30CharsetEncoder uxec = Charset.forName("UTF-16BE").newEncoder();
     31CharsetEncoder u8ec = Charset.forName("UTF-8").newEncoder();
     32ByteBuffer gbbb = null;
     33ByteBuffer uxbb = null;
     34ByteBuffer u8bb = null;
     35CharBuffer cb = null;
     36int count = 0;
     37for (int i=1; i<=94; i++{
     38// Defining row settings
     39if (i>=1 && i<=9{
     40reserved = false;
     41name = "Graphic symbols";
     42}
     else if (i>=10 && i<=15{
     43reserved = true;
     44name = "Reserved";
     45}
     else if (i>=16 && i<=55{
     46reserved = false;
     47name = "Level 1 characters";
     48}
     else if (i>=56 && i<=87{
     49reserved = false;
     50name = "Level 2 characters";
     51}
     else if (i>=88 && i<=94{
     52reserved = true;
     53name = "Reserved";
     54}

     55// writing row title
     56writeln();
     57writeString("<p>");
     58writeNumber(i);
     59writeString(" Row: "+name);
     60writeln();
     61writeString("</p>");
     62writeln();
     63if (!reserved) {
     64writeln();
     65writeHeader();
     66// looping through all characters in one row
     67for (int j=1; j<=94; j++{
     68byte hi = (byte)(0xA0 + i);
     69byte lo = (byte)(0xA0 + j);
     70if (validGB(i,j)) {
     71// getting GB, UTF-16BE, UTF-8 codes
     72gbbb = ByteBuffer.wrap(new byte[]{hi,lo});
     73try {
     74cb = gbdc.decode(gbbb);
     75uxbb = uxec.encode(cb);
     76cb.rewind();
     77u8bb = u8ec.encode(cb);
     78}
     catch (CharacterCodingException e) {
     79cb = null;
     80uxbb = null;
     81u8bb = null;
     82}

     83}
     else {
     84cb = null;
     85uxbb = null;
     86u8bb = null;
     87}

     88writeNumber(i);
     89writeNumber(j);
     90writeString(" ");
     91if (cb!=null{
     92writeByte(hi);
     93writeByte(lo);
     94writeString(" ");
     95writeHex(hi);
     96writeHex(lo);
     97count++;
     98}
     else {
     99writeGBSpace();
    100writeString(" null");
    101}

    102writeString(" ");
    103writeByteBuffer(uxbb,2);
    104writeString(" ");
    105writeByteBuffer(u8bb,3);
    106if (j%2 == 0{
    107writeln();
    108}
     else {
    109writeString("   ");
    110}

    111}

    112writeFooter();
    113}

    114}

    115System.out.println("Number of GB characters worte: "+count);
    116}

    117public static void writeln() throws IOException {
    118out.write(0x0D);
    119out.write(0x0A);
    120}

    121public static void writeByte(byte b) throws IOException {
    122out.write(b & 0xFF);
    123}

    124public static void writeByteBuffer(ByteBuffer b, int l)
    125throws IOException {
    126int i = 0;
    127if (b==null{
    128writeString("null");
    129= 2;
    130}
     else {
    131for (i=0; i<b.limit(); i++) writeHex(b.get(i));
    132}

    133for (int j=i; j<l; j++) writeString("  ");
    134}

    135public static void writeGBSpace() throws IOException {
    136out.write(0xA1);
    137out.write(0xA1);
    138}

    139public static void writeString(String s) throws IOException {
    140if (s!=null{
    141for (int i=0; i<s.length(); i++{
    142out.write((int) (s.charAt(i) & 0xFF));
    143}

    144}

    145}

    146public static void writeNumber(int i) throws IOException {
    147String s = "00" + String.valueOf(i);
    148writeString(s.substring(s.length()-2,s.length()));
    149}

    150public static void writeHex(byte b) throws IOException {
    151out.write((int) hexDigit[(b >> 4& 0x0F]);
    152out.write((int) hexDigit[b & 0x0F]);
    153}

    154public static void writeHeader() throws IOException {
    155writeString("<pre>");
    156writeln();
    157writeString("Q.W. ");
    158writeGBSpace();
    159writeString(" GB   Uni. UTF-8 ");
    160writeString("   ");
    161writeString("Q.W. ");
    162writeGBSpace();
    163writeString(" GB   Uni. UTF-8 ");
    164writeln();
    165writeln();
    166}

    167public static void writeFooter() throws IOException {
    168writeString("</pre>");
    169writeln();
    170}

    171public static boolean validGB(int i,int j) {
    172for (int l=0; l<b_out.length; l++{
    173if (i*100+j>=b_out[l] && i*100+j<=e_out[l]) return false;
    174}

    175return true;
    176}

    177}
  • 相关阅读:
    hotmail 收不到邮件的问题
    getaddrinfo 报错 Invalid value for ai_flags
    Avoiding Common Networking Mistakes
    关掉标准输出
    不需要 root 权限的 ping
    select 的问题
    Behavior Tree 用 Lua 实现一个最简行为树
    对 UDP 的一些思考
    Windows UDP sockets: recvfrom() fails with error 10054
    和等于某个数的所有组合
  • 原文地址:https://www.cnblogs.com/meil/p/635873.html
Copyright © 2011-2022 走看看