六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 37|回复: 0

中文转化为unicode

[复制链接]

升级  47.33%

33

主题

33

主题

33

主题

秀才

Rank: 2

积分
121
 楼主| 发表于 2013-2-4 20:02:39 | 显示全部楼层 |阅读模式
原文地址:
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:

//转为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") %>
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表