중국어 가 유 니 코드 로 바 뀌 었 습 니 다.

4361 단어 자바C++cjspC#
원본 주소:
http://www.cnitblog.com/neatstudio/archive/2006/07/28/14315.html
js
mode="zhuan";

function encode(obj,btn){
   if(mode=="zhuan"){
       obj.value=obj.value.replace(/[^\u0000-\u00FF]/g,function($0){return escape($0).replace(/(%u)(\w{4})/gi,"&#x$2;")});
       btn.value="  ";
       mode="huan";
   }else{
       obj.value=unescape(obj.value.replace(/&#x/g,'%u').replace(/;/g,''));
       btn.value="  ";
       mode="zhuan";
   }
}

JAVA:
/ / 유 니 코드 로 전환
public static void writeUnicode(final DataOutputStream out, final String value)  {
  try {
  final String unicode = gbEncoding( value );
  final byte[] data = unicode.getBytes();
  final int dataLength = data.length;

  System.out.println( "Data Length is: " + dataLength );
  System.out.println( "Data is: " + value );
  out.writeInt( dataLength ); //         
  out.write( data, 0, dataLength ); //           
  } catch (IOException e) {
 
  }
  }

 
  public static String gbEncoding( final String gbString ) {
  char[] utfBytes = gbString.toCharArray();
  String unicodeBytes = "";
  for( int byteIndex = 0; byteIndex < utfBytes.length; byteIndex ++ ) {
  String hexB = Integer.toHexString( utfBytes[ byteIndex ] );
  if( hexB.length() <= 2 ) {
  hexB = "00" + hexB;
  }
  unicodeBytes = unicodeBytes + "\\\\u" + hexB;
  }
  System.out.println( "unicodeBytes is: " + unicodeBytes );
  return unicodeBytes;
  }


/** *//*****************************************************
  *     : unicode       
  *     : unicode   
  *     :       
  *****************************************************/
 private String decodeUnicode( final String dataStr ) {
  int start = 0;
  int end = 0;
  final StringBuffer buffer = new StringBuffer();
  while( start > -1 ) {
  end = dataStr.indexOf( "\\\\u", start + 2 );
  String charStr = "";
  if( end == -1 ) {
  charStr = dataStr.substring( start + 2, dataStr.length() );
  } else {
  charStr = dataStr.substring( start + 2, end);
  }
  char letter = (char) Integer.parseInt( charStr, 16 ); // 16  parse     。
  buffer.append( new Character( letter ).toString() );
  start = end;
  }
  return buffer.toString();
 }
 

JSP:

/** *//** ToUnicode.java */ 
package com.edgewww.util; 

import java.io.*; 

/** *//** 
*       Unicode    
* @author     [email protected] 
* @date 2001-03-05 
*/ 
public class ToUnicode { 

/** *//** 
*        Unicode  
* @param strText         
* @param code          , "GBK" 
* @return     Unicode     
*/ 
public String toUnicode(String strText,String code) throws UnsupportedEncodingException{ 
  char c; 
  String strRet = "" ; 
  int intAsc; 
  String strHex; 
  strText = new String(strText.getBytes("8859_1"),code); 
  for ( int i = 0; i < strText.length(); i++ ){ 
    c = strText.charAt(i); 
    intAsc = (int)c; 
    if(intAsc>128){ 
      strHex = Integer.toHexString(intAsc); 
      strRet = strRet + "&#x" + strHex+";"; 
    } 
    else{ 
      strRet = strRet + c; 
    } 
  } 
  return strRet ; 
} 

} 

/** *//**      */ 
/** *//** gbk2Unicode.jsp */ 
<meta http-equiv="Content-Type" content="text/html; charset=big5"> 
<jsp:useBean id="g2u" scope="session" class="com.edgewww.util.ToUnicode"/> 
<% String lang = "      "; %> 
<br> 
<%=lang %> 
<br> 
<%=g2u.toUnicode(lang,"GBK") %>

좋은 웹페이지 즐겨찾기