Character Set Tool class

Source: Internet
Author: User
Tags 0xc0

Package Test;import Java.io.unsupportedencodingexception;import Java.net.urlencoder;import java.net.URLDecoder;    public class Chartools {public string iso2gb (string text) {string result = "";    try {result = new String (text.getbytes ("iso-8859-1"), "GB2312");    } catch (Unsupportedencodingexception ex) {result = Ex.tostring ();  } return result;    public string Gb2iso (string text) {string result = "";    try {result = new String (text.getbytes ("GB2312"), "iso-8859-1");    } catch (Unsupportedencodingexception ex) {ex.printstacktrace ();  } return result;    public string Utf8urlencode (string text) {StringBuffer result = new StringBuffer ();      for (int i = 0; i < text.length (); i++) {char c = text.charat (i);      if (c >= 0 && C <= 255) {result.append (c);        }else {byte[] b = new Byte[0];        try {b = character.tostring (c). GetBytes ("UTF-8");  }catch (Exception ex) {      } for (int j = 0; J < B.length; J + +) {int k = b[j];          if (k < 0) k + = 256;        Result.append ("%" + integer.tohexstring (k). toUpperCase ());  }}} return Result.tostring ();    public string Utf8urldecode (string text) {string result = "";    int p = 0;      if (Text!=null && text.length () >0) {text = Text.tolowercase ();      p = text.indexof ("%e");      if (p = =-1) return text;        while (P! =-1) {result + = text.substring (0, p);        Text = Text.substring (P, text.length ()); if (Text = = "" | |        Text.length () < 9) return result;        Result + = Codetoword (text.substring (0, 9));        Text = text.substring (9, Text.length ());      p = text.indexof ("%e");  }} return result + text;    } private String Codetoword (string text) {string result;      if (Utf8codecheck (text)) {byte[] code = new BYTE[3];    Code[0] = (byte) (Integer.parseint (text.substring (1, 3), 16)-256);  CODE[1] = (byte) (Integer.parseint (Text.substring (4, 6), 16)-256);      CODE[2] = (byte) (Integer.parseint (Text.substring (7, 9), 16)-256);      try {result = new String (code, "UTF-8");      }catch (Unsupportedencodingexception ex) {result = null;    }} else {result = text;  } return result;    } public static Boolean IsValidUtf8 (byte[] b, int amaxcount) {int llen = b.length, lcharcount = 0;       for (int i = 0; i < Llen && Lcharcount < Amaxcount; ++lcharcount) {byte lbyte = b[i++]; if (lbyte >= 0) continue;      >=0 is normal ASCII if (Lbyte < (byte) 0xc0 | | lbyte > (BYTE) 0xfd) return false; int lCount = lbyte > (byte) 0xfc? 5:lbyte > (byte) 0xf8? 4:lbyte > (byte) 0xf0? 3:lbyte > (byte) 0xe0?      2:1;      if (i + LCount > Llen) return false;    for (int j = 0; j < LCount; ++j, ++i) if (B[i] >= (byte) 0xc0) return false;  } return true; } PrivaTe boolean Utf8codecheck (String text) {String sign = "";        if (Text.startswith ("%e")) for (int i = 0, p = 0; P! =-1; i++) {p = text.indexof ("%", p);        if (P! =-1) p++;      Sign + = P;  } return Sign.equals ("147-1"); }  /**   * ???   Utf8url????    * @param text * @return */public boolean isutf8url (String text) {text = Text.tolowercase ();    int p = text.indexof ("%");    if (P! =-1 && text.length ()-p > 9) {text = Text.substring (p, p + 9);  } return Utf8codecheck (text);   }  /**   * ????    * @param args */public static void main (string[] args) {Chartools chartools = new Chartools ();    CHARTOOLS.ISO2GB ("Yabushan");    Chartools.gb2iso ("Yabushan");    TODO String URL; url = "http://www.google.com/search?hl=zh-CN&newwindow=1&q=%E4%B8%AD%E5%9B%BD%E5%A4%A7%E7%99%BE%E7%A7%91   %e5%9c%a8%e7%ba%bf%e5%85%a8%e6%96%87%e6%a3%80%e7%b4%a2&btng=%e6%90%9c%e7%b4%a2&lr= "; Chartools.utf8urlencode (UrL);   Chartools.utf8urldecode (URL);                Chartools.utf8codecheck (URL);    if (Chartools.isutf8url (URL)) {System.out.println (Chartools.utf8urldecode (URL) + ">>>>");    }else{System.out.println (Urldecoder.decode (URL)); } URL = "http://www.baidu.com/baidu?word=%d6%d0%b9%fa%b4%f3%b0%d9%bf%c6%d4%da%cf%df%c8%ab%ce%c4%bc%ec%cb%f7&    TN=MYIE2DG ";    if (Chartools.isutf8url (URL)) {System.out.println (Chartools.utf8urldecode (URL));    }else{System.out.println (Urldecoder.decode (URL)); }  }}

  

Character Set Tool class

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.