中文转化为unicode

原文地址:
[url]http://www.cnitblog.com/neatstudio/archive/2006/07/28/14315.html[/url]
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:

//转为unicode
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 栾金奎 jsp@shanghai.com
* @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") %>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值