隨筆 - 22  文章 - 467  trackbacks - 0
          <2025年5月>
          27282930123
          45678910
          11121314151617
          18192021222324
          25262728293031
          1234567

          常用鏈接

          留言簿(25)

          隨筆分類(74)

          文章分類(1)

          收藏夾(277)

          在線工具

          在線資料

          最新隨筆

          搜索

          •  

          積分與排名

          • 積分 - 217585
          • 排名 - 261

          最新評(píng)論

          閱讀排行榜

          評(píng)論排行榜

          有些網(wǎng)站,特別是有些網(wǎng)站的手機(jī)版,喜歡直接輸出類似
          &# x6B22;&# x8FCE;&# x6765;&# x5230;Aloong&# x7684;Java&# x535A;&# x5BA2;!
          (全部插入了空格避免被轉(zhuǎn)義,下面的代碼中也都加了空格)
          這類16進(jìn)制網(wǎng)頁(yè)編碼,雖然我們的瀏覽器可以顯示出正確的文字,但是看源代碼的時(shí)候就是滿眼的亂碼了.
          于是在需要寫處理網(wǎng)頁(yè)代碼的程序就可能遇到轉(zhuǎn)換這些編碼的問(wèn)題.

          以下分享兩個(gè)我轉(zhuǎn)換這種編碼的兩個(gè)例子.

          1. 解碼:
          import java.util.regex.Matcher;
          import java.util.regex.Pattern;

          /**
           * @ClassName: TestDecode 
           * @Description: 解碼16進(jìn)制網(wǎng)頁(yè)編碼的例子 
           * 
          @author Aloong 
           * @date 2010-10-25 上午02:19:59 
           * 
           
          */

          public class TestDecode
          {

              
          public static void main(String[] args)
              
          {
                  String pageCode 
          = "<span>&# x6B22;&# x8FCE;&# x6765;&# x5230;Aloong&# x7684;Java&# x535A;&# x5BA2;!</span>";
                  Pattern p;
                  Matcher m;
                  
                  
          //將"&# x6B22;"轉(zhuǎn)換為"\u6B22"
                  p = Pattern.compile("&#x[\\d\\w]{4};",Pattern.DOTALL);
                  m 
          = p.matcher(pageCode);
                  
          while (m.find())
                  
          {
                      String group 
          = m.group();
                      pageCode 
          = pageCode.replaceAll(group, "\\\\u" + group.substring(37).toUpperCase());
                  }

                  
          //去掉html標(biāo)簽的正則表達(dá)式
                  pageCode = pageCode.replaceAll("<[^>]*>""");
                  
          //解碼:
                  pageCode = loadConvert(pageCode);
                  
                  System.out.println(pageCode);
              }

              
              
          private static String loadConvert(String theString)
              
          {
                  
          char aChar;
                  
          int len = theString.length();
                  StringBuffer outBuffer 
          = new StringBuffer(len);
                  
          for (int x = 0; x < len;)
                  
          {
                      aChar 
          = theString.charAt(x++);
                      
          if (aChar == '\\')
                      
          {
                          aChar 
          = theString.charAt(x++);
                          
          if (aChar == 'u')
                          
          {
                              
          int value = 0;
                              
          for (int i = 0; i < 4; i++)
                              
          {
                                  aChar 
          = theString.charAt(x++);
                                  
          switch (aChar)
                                  
          {
                                  
          case '0':
                                  
          case '1':
                                  
          case '2':
                                  
          case '3':
                                  
          case '4':
                                  
          case '5':
                                  
          case '6':
                                  
          case '7':
                                  
          case '8':
                                  
          case '9':
                                      value 
          = (value << 4+ aChar - '0';
                                      
          break;
                                  
          case 'a':
                                  
          case 'b':
                                  
          case 'c':
                                  
          case 'd':
                                  
          case 'e':
                                  
          case 'f':
                                      value 
          = (value << 4+ 10 + aChar - 'a';
                                      
          break;
                                  
          case 'A':
                                  
          case 'B':
                                  
          case 'C':
                                  
          case 'D':
                                  
          case 'E':
                                  
          case 'F':
                                      value 
          = (value << 4+ 10 + aChar - 'A';
                                      
          break;
                                  
          default:
                                      
          throw new IllegalArgumentException(
                                              
          "Malformed   \\uxxxx   encoding.");
                                  }

                              }

                              outBuffer.append((
          char) value);
                          }
           else //可選是否轉(zhuǎn)換這些字符
                          {
                              
          if (aChar == 't')
                                  aChar 
          = '\t';
                              
          else if (aChar == 'r')
                                  aChar 
          = '\r';
                              
          else if (aChar == 'n')
                                  aChar 
          = '\n';
                              
          else if (aChar == 'f')
                                  aChar 
          = '\f';
                              outBuffer.append(aChar);
                          }

                      }
           else
                          outBuffer.append(aChar);
                  }

                  
          return outBuffer.toString();
              }


          }
          以上程序可以輸出:"歡迎來(lái)到Aloong的Java博客!"

          2. 編碼:
          /**
           * @ClassName: TestEncode 
           * @Description: 編碼16進(jìn)制網(wǎng)頁(yè)編碼的例子 
           * 
          @author Aloong 
           * @date 2010-10-25 上午02:31:58 
           * 
           
          */

          public class TestEncode
          {

              
          public static void main(String[] args)
              
          {
                  String str 
          = "歡迎來(lái)到Aloong的Java博客!";

                  str 
          = toUnicode(str, false);

                  System.out.println(str);
              }


              
          public static String toUnicode(String theString, boolean escapeSpace)
              
          {
                  
          int len = theString.length();
                  
          int bufLen = len * 2;
                  
          if (bufLen < 0)
                  
          {
                      bufLen 
          = Integer.MAX_VALUE;
                  }

                  StringBuffer outBuffer 
          = new StringBuffer(bufLen);

                  
          for (int x = 0; x < len; x++)
                  
          {
                      
          char aChar = theString.charAt(x);
                      
          // Handle common case first, selecting largest block that
                      
          // avoids the specials below
                      if ((aChar > 61&& (aChar < 127))
                      
          {
                          
          if (aChar == '\\')
                          
          {
                              outBuffer.append(
          '\\');
                              outBuffer.append(
          '\\');
                              
          continue;
                          }

                          outBuffer.append(aChar);
                          
          continue;
                      }

                      
          switch (aChar)
                      
          {
                      
          case ' ':
                          
          if (x == 0 || escapeSpace)
                              outBuffer.append(
          '\\');
                          outBuffer.append(
          ' ');
                          
          break;
                      
          case '\t':
                          outBuffer.append(
          '\\');
                          outBuffer.append(
          't');
                          
          break;
                      
          case '\n':
                          outBuffer.append(
          '\\');
                          outBuffer.append(
          'n');
                          
          break;
                      
          case '\r':
                          outBuffer.append(
          '\\');
                          outBuffer.append(
          'r');
                          
          break;
                      
          case '\f':
                          outBuffer.append(
          '\\');
                          outBuffer.append(
          'f');
                          
          break;
                      
          //可選是否轉(zhuǎn)義這些字符
          //            case '=': // Fall through
          //            case ':': // Fall through
          //            case '#': // Fall through
          //            case '!':
          //                outBuffer.append('\\'); outBuffer.append(aChar);
          //                break;
                      default:
                          
          if ((aChar < 0x0020|| (aChar > 0x007e))
                          
          {
                              
          //添加前綴"&#x",可以改成其他前綴如"\\u"
                              outBuffer.append("&#x");
                              outBuffer.append(toHex((aChar 
          >> 12& 0xF));
                              outBuffer.append(toHex((aChar 
          >> 8& 0xF));
                              outBuffer.append(toHex((aChar 
          >> 4& 0xF));
                              outBuffer.append(toHex(aChar 
          & 0xF));
                              
          //添加后綴";"
                              outBuffer.append(';');
                          }
           else
                          
          {
                              outBuffer.append(aChar);
                          }

                      }

                  }

                  
          return outBuffer.toString();
              }


              
          //查詢16進(jìn)制對(duì)應(yīng)表
              private static char toHex(int nibble)
              
          {
                  
          return hexDigit[(nibble & 0xF)];
              }

              
          private static final char[] hexDigit =
              
          '0''1''2''3''4''5''6''7''8''9''A''B''C''D',
                      
          'E''F' }
          ;

          }
          以上程序輸出:
          &# x6B22;&# x8FCE;&# x6765;&# x5230;Aloong&# x7684;Java&# x535A;&# x5BA2;!
          (這里在x的前面都加了空格,不然會(huì)被瀏覽器自動(dòng)轉(zhuǎn)成文字的.)
          以上兩個(gè)轉(zhuǎn)換代碼也是網(wǎng)上找到了,只是自己稍作修改并寫了例子.
          posted on 2010-10-25 02:48 ApolloDeng 閱讀(2023) 評(píng)論(1)  編輯  收藏 所屬分類: 分享JavaWeb

          FeedBack:
          # re: 類似"&# x6B22;"這類16進(jìn)制網(wǎng)頁(yè)編碼的編碼與解碼方法 2011-12-10 21:52 tbw淘寶商城
          我這就去試試。  回復(fù)  更多評(píng)論
            
          主站蜘蛛池模板: 巴林左旗| 嵊泗县| 正蓝旗| 乐亭县| 文登市| 镇平县| 桃江县| 铜梁县| 鄂州市| 故城县| 灵武市| 平原县| 屏东县| 龙山县| 阿巴嘎旗| 静宁县| 宁都县| 会宁县| 安庆市| 米林县| 介休市| 大悟县| 金塔县| 乌苏市| 东宁县| 安岳县| 三原县| 大名县| 宁河县| 类乌齐县| 遵化市| 都安| 麦盖提县| 托里县| 保亭| 镇巴县| 常德市| 霞浦县| 肥乡县| 大竹县| 海宁市|