JAVA JS escape function, parsing with JS encodeURI encoded string, UTF8 to gb2312 function

Source: Internet
Author: User

When using WebView, if the parameters in the URL are in Chinese, the intercepted string will resemble this: http://api.letstar.cn/zq/news.html?id=20&cupName=%E6%B5%8B%E8%AF% 957, what if I want to use Cupname?

public static string Escape (string src) {int I;int J; StringBuffer tmp = new StringBuffer () tmp.ensurecapacity (Src.length () * 6); for (i = 0; i < src.length (); i++) {j = src. CharAt (i); if (J < n) {tmp.append ("%"), if (J < tmp.append) ("0"), Tmp.append (Integer.tostring (J, 16));} else {tmp.append ("%u"); Tmp.append (Integer.tostring (J, 16));}} return tmp.tostring ();}

  

String str= "k.a%c3%a4%c2%b8%c2%b4%c3%a6%c2%97%c2%b6%c3%a4%c2%be%c2%9b%c3%a5%c2%ba%c2%94%c3%a5%c2%95%c2%86";// Str=escape (Java.net.URLDecoder.decode (str, "Utf-8"))//system.out.println (Java.net.URLDecoder.decode (str, "Utf-8 ")); str= (Java.net.URLDecoder.decode (str," utf-8 ")); SYSTEM.OUT.PRINTLN (utf8togb2312 (str));

Analytical:

private static string utf8togb2312 (String str) {StringBuffer SB = new Stringbuff      ER ();          for (int i=0; i<str.length (); i++) {char c = str.charat (i);             Switch (c) {case ' + ': sb.append (');             Break Case '% ': try {sb.append ((char) integer.parseint (str.substring (                 I+1,I+3), 16));                } catch (NumberFormatException e) {throw new illegalargumentexception ();                } i + = 2;             Break                Default:sb.append (c);           Break      }}//Undo conversion to external encoding String result = sb.tostring ();      String Res=null;          try{byte[] inputbytes = result.getbytes ("8859_1");      res= New String (inputbytes, "UTF-8"); } catch (Exception e) {} return res;} 

  

JAVA JS escape function, parsing with JS encodeURI encoded string, UTF8 to gb2312 function

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.