-
- /*
- ConvertBig5Unicode.java
-
- print string in big5 and unicode
-
- >javac ConvertBig5Unicode.java
- >java ConvertBig5Unicode
- 查詢字串: 今天天氣很好
- 查詢字串的統一碼 : 4eca 5929 5929 6c23 5f88 597d
- 查詢字串的大五碼 : a4b5 a4d1 a4d1 aef0 abdc a66e
- */
-
- import java.io.File;
-
- // 如何取得java字串之unicode及big5碼
- public class ConvertBig5Unicode
- {
- public static void main(String args[]) throws java.io.UnsupportedEncodingException
- {
- String query="今天天氣很好";
-
- String unicode_query="";
- for(int i=0; i<=query.length()-1;i++)
- {
- if(query.charAt(i)>=0x100)
- unicode_query += Integer.toHexString(query.charAt(i)) + " ";
- else
- unicode_query += query.charAt(i);
- }
-
- byte [] big5_stream=query.getBytes("big5");
- // java.io.UnsupportedEncodingException
-
- String big5_query="";
- for(int i=0; i<=big5_stream.length-1;i++)
- {
- int b1,b2;
-
- b1 = big5_stream[i];
- b2 = (i+1<=big5_stream.length-1)? big5_stream[i+1] : 0;
-
- b1 = (b1>=0) ? b1 : 0x100+b1;
- b2 = (b2>=0) ? b2 : 0x100+b2;
-
- //out.println("b1="+b1+",b2="+b2);
-
- if((b1 >= 0xa1 && b1 <= 0xf9) && ((b2 >= 0x40 && b2 <= 0x7e) || (b2 >= 0xa1 && b2 <= 0xfe)))
- {
- big5_query += Integer.toHexString(b1) + Integer.toHexString(b2) + " ";
- i++;
- }
- else
- big5_query += (char) big5_stream[i];
- }
-
- System.out.println("查詢字串: " + query);
- System.out.println("查詢字串的統一碼 : "+unicode_query);
- System.out.println("查詢字串的大五碼 : "+big5_query);
- }
- }
2008年12月2日 星期二
how to get unicode/big5 code in java String
訂閱:
張貼留言 (Atom)
沒有留言:
張貼留言