JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 程式分享區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to postflat modego to previous topicgo to next topic
話題被移動
該話題已被移動 - caterpillar , 2004-02-20 21:29
如果您尚不清楚該話題被移動的原因,請參考論壇規則以及本版公告或者聯系本版版主。
本主題所含的標籤
無標籤
作者 [工具]Big5 to Unicode and Unicode to Big5
weaker

小屁屁∼



發文: 379
積分: 4
於 2004-02-16 16:07 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
剛好有用到
所以寫了一下
可能不好,還希望大家多多指教
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import java.io.*;
import java.awt.*;
import java.awt.event.*;
 
import javax.swing.*;
 
public class big5ToUni implements ActionListener {
 
  private JButton utoBButton = null;
  private JButton btoUButton = null;
  private JButton clearButton = null;
  private TextArea content = null;
  private JLabel statusLabel = null;
  private JLabel status = null;
  
  public big5ToUni() {
    JFrame f = new JFrame("Big5 <==> Unicode");
    f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    Container c = f.getContentPane();
    c.setLayout(new GridBagLayout());
    GridBagConstraints cons = new GridBagConstraints();
    cons.gridx = 0; cons.gridy = 0;
    cons.gridwidth = 1;
    c.add(getContent(), cons);
    cons.gridy = 1;
    c.add(getButton(), cons);
    cons.gridy = 2;
    c.add(getStatus(), cons);
    f.pack();
    f.show();
  }
  
  public void actionPerformed(ActionEvent e) {
    String str =content.getText();
    if (e.getActionCommand().equals("Big5 to Uni")) {
      if (str.length() == 0) {
        status.setText("No String Input !");
        return;
      } else {
        changeToUni(str);
        status.setText("Transform Completed !");
      }
    } else if (e.getActionCommand().equals("Clear")) {
        content.setText("");
        return;
    } else if (e.getActionCommand().equals("Uni to Big5")) {
      if (str.length() == 0) {
        status.setText("No String Input !");
        return;
      } else {
        changeToBig5(str);
        status.setText("Transform Completed !");
      }
      
    }
  }
  
  // transform to big5
  public void changeToBig5(String str) {
    str = str.replaceAll("\\u005cu","");
    byte[] tmpStr = new byte[str.length()/2];
    for (int i = 0;i < tmpStr.length; i++) {
      tmpStr[i] = Integer.valueOf(str.substring(2*i,2*(i+1)),16).byteValue();
    }
    try {
      content.setText(new String(tmpStr,"UTF-16"));
    } catch (Exception ignored) {}
  }
  
  // transform to Unicode
    public void changeToUni(String str) {
      int countChar = str.length();
      StringBuffer sb = new StringBuffer();
      try {
        for (int i = 0;i < countChar;i++) {
          // don't transform '\r' , '\n'
          if (str.substring(i,i+1).equals("\r") || str.substring(i,i+1).equals("\n")) { 
            sb.append("\n");
          } else {
            // encode by using utf-16
            byte[] uni_str = str.substring(i,i+1).getBytes("utf-16");
            sb.append("\\u");
            // start to transform to unicode 
            for (int j = 2; j < uni_str.length; j++) {
              int tmp = uni_str[j];
              if (tmp < 0) {
                tmp = 256 + tmp; 
                if (tmp < 16) 
                  sb.append("0" + Integer.toHexString(tmp));
                else 
                  sb.append(Integer.toHexString(tmp));          
              } else if (tmp < 16) {
                sb.append("0" + Integer.toHexString(tmp));
              } else {
                sb.append(Integer.toHexString(tmp));
              }
            }
          }
        }
        content.setText(sb.toString());
      } catch (UnsupportedEncodingException e1) {
        e1.printStackTrace();
      }    
    }
    
    public JPanel getButton() {
      JPanel button = new JPanel();
      utoBButton = new JButton("Uni to Big5");
      btoUButton = new JButton("Big5 to Uni");
      clearButton = new JButton("Clear");
      utoBButton.addActionListener(this);
      btoUButton.addActionListener(this);
      clearButton.addActionListener(this);
      button.add(clearButton);
      button.add(utoBButton);
      button.add(btoUButton);
      return button;
    }
    
    public JScrollPane getContent() {
      content = new TextArea(10,30);
      JScrollPane scrollContent = new JScrollPane(content);
      return scrollContent;
    }
    
    public JPanel getStatus() {
      JPanel statusPanel = new JPanel();
      statusPanel.setLayout(new GridBagLayout());
      GridBagConstraints cons = new GridBagConstraints();
      statusLabel = new JLabel("Status : ");
      status = new JLabel("wait to transform.....");
      cons.gridx = 0; cons.gridy = 0;
      statusPanel.add(statusLabel, cons);
      cons.gridx = 1; cons.gridwidth = 2;
      statusPanel.add(status, cons);
      return statusPanel;
    }
    public static void main(String[] args) {
      new big5ToUni();
    }    
}
 

以下為原始檔

big5toUni.jar (4.34k)


caterpillar edited on 2004-03-04 12:22
reply to postreply to post
話題樹型展開
人氣 標題 作者 字數 發文時間
12825 [工具]Big5 to Unicode and Unicode to Big5 weaker 4639 2004-02-16 16:07
7834 Re:分享 : Big5 to Unicode and Unicode to Big5 Duncan 164 2004-02-16 16:15
7404 Re:分享 : Big5 to Unicode and Unicode to Big5 weaker 172 2004-02-16 16:22
8224 Re:分享 : Big5 to Unicode and Unicode to Big5 popcorny 80 2004-02-16 16:23
11420 Re:分享 : Big5 to Unicode and Unicode to Big5 Duncan 231 2004-02-16 17:20
» JWorld@TW »  Java 程式分享區

reply to postflat modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8