用java实现native2asscii命令

  1. packagecom.work.util;
  2. importjava.io.UnsupportedEncodingException;
  3. /**
  4. *准备在动态的生成javascript的时候使用。
  5. *@authorwangmingjie
  6. *
  7. *native2ascii命令的java实现!
  8. *
  9. */
  10. publicclassNative2Ascii{
  11. /**
  12. *可以将GBK转换成为UTF-8编码
  13. *@paramstr
  14. *@return
  15. */
  16. publicstaticStringnative2ascii(Stringstr){
  17. char[]ac=str.toCharArray();
  18. StringBufferrs=newStringBuffer(ac.length);
  19. for(intk=0;k<ac.length;k++)
  20. if(ac[k]>'\177'){
  21. rs.append((char)92);
  22. rs.append((char)117);
  23. Strings1=Integer.toHexString(ac[k]);
  24. StringBufferstringbuffer=newStringBuffer(s1);
  25. stringbuffer.reverse();
  26. intl=4-stringbuffer.length();
  27. for(inti1=0;i1<l;i1++)
  28. stringbuffer.append('0');
  29. for(intj1=0;j1<4;j1++)
  30. rs.append(stringbuffer.charAt(3-j1));
  31. }else{
  32. rs.append(ac[k]);
  33. }
  34. returnrs.toString();
  35. }
  36. publicstaticvoidmain(String[]args)throwsUnsupportedEncodingException{
  37. System.out.println(Native2Ascii.native2ascii("我们是中国人"));
  38. System.out.println("\u6211\u4eec\u662f\u4e2d\u56fd\u4eba");
  39. System.out.println(Native2Ascii.native2ascii("完整性检查.raq"));
  40. System.out.println(java.net.URLEncoder.encode("完整性检查.raq","gbk"));
  41. System.out.println(java.net.URLDecoder.decode("%CD%EA%D5%FB%D0%D4%BC%EC%B2%E9.raq","gbk"));
  42. System.out.println(java.net.URLEncoder.encode("中国","gbk"));
  43. }
  44. }

你可能感兴趣的:(JavaScript,java,.net)