中文转为unicode编码
来源:互联网 发布:佛山网页美工培训 编辑:程序博客网 时间:2024/05/09 17:49
中文转为unicode编码
什么是Unicode:
在创造Unicode之前针对各种语言有几百种编码系统,而且这些编码系统也相互冲突,给不同语言系统的电脑进行交流带来了麻烦。因为两种相同的字符在不同的编码系统可能有完全不同的意思,这些不同甚至会对电脑带来危害。于是Unicode出现了,Unicode给每个字符提供了一个唯一的数字,不论是什么平台,不论是什么程序,不论是什么语言。它真正实现了全球电脑系统的United,作为一个标准,它已经成为全球软件技术最重要的发展趋势。
为什么要把中文转换为Unicode
在互联网高速发展的今天,Unicode担当更重要的角色, 它比传统的字符编码更节省费用,使软件或者网站能够运用于不同的系统平台、语言和国家,而不需要重建,同时也保证了资料在不同系统中的完整性。所以说你只要将中文转换为Unicode,任何国家的人都能看到你想表达的真正意思,而不是乱码。
举例
中文原码(GB2312):叁肆伍陆柒捌
转为Unicode后: 叁肆伍陆柒捌网页效果为:叁肆伍陆柒捌
提示:改变网页编码为其它任何国家、语言的编码试试,始终能看见上面红色的Unicode中文。
var 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";
}
}
未知:
Function Str_Gb2UniCode(text:String):String;
var
i,j,len:integer;
cur:integer;
t:String;
ws:widestring;
begin
Result:='';
ws:=text;
len:=length(ws);
i:=1;
j:=0;
while i<=len do
begin
cur:=ord(ws[i]);
FmtStr(t,'%4.4X',[cur]);//BCD转换
Result:=Result+t;
inc(i);
j:=(j+1)mod7;//移位计数器达7要特殊处理
end;
end;
JAVA:
将中文转为unicode 及转回中文函数
//转为unicodepublic 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")%>
- 中文转为unicode编码
- 中文转为unicode编码
- 字符转为unicode编码
- 中文编码、unicode、全角
- Unicode编码:保存中文
- Unicode编码:保存中文
- Unicode编码转中文
- 中文转成Unicode编码
- 字符转为Unicode编码的小方法
- Unicode编码的byte 数组转为String
- 中文转为unicode的字符串形式
- Java实现Unicode码转为中文
- PHP 中文 转UNICODE, JS 将UNICODE转为GBK
- Unicode解码(unicode编码转中文)
- 中文转换为Unicode编码-Unicode编码转换为中文
- Ubuntu下实现UTF8编码转为Unicode编码 C程序
- 中文信息转Unicode编码
- eclipse中文编码转unicode
- SQL脚本积累之六-----游标的使用示例
- 链表练习
- 2.2 什么是lambda函数?它有什么好处?
- 黑马程序员——Java基础---io流(io中的其他对象)
- 各种传送门
- 中文转为unicode编码
- webservice xml soap wsdl 必要知识
- Latch-Learned Arrangements of Three Patch Codes (1)
- 初步认识机房收费
- HDU 2032 杨辉三角
- error LNK2019: 无法解析的外部符号 ___glutInitWithExit@12,该符号在函数 _glutInit_ATEXIT_HACK@8 中被引用 1>GEARS.obj : e
- hashMap
- 基于 Web 的 Go 语言 IDE - Wide 1.3.0 发布!
- java多线程之停止线程