-
什么是Unicode:
在创造Unicode之前针对各种语言有几百种编码系统,而且这些编码系统也相互冲突,给不同语言系统的电脑进行交流带来了麻烦。因为两种相同的字符在不同的编码系统可能有完全不同的意思,这些不同甚至会对电脑带来危害。于是Unicode出现了,Unicode给每个字符提供了一个唯一的数字,不论是什么平台,不论是什么程序,不论是什么语言。它真正实现了全球电脑系统的United,作为一个标准,它已经成为全球软件技术最重要的发展趋势。 -
为什么要把中文转换为Unicode
在互联网高速发展的今天,Unicode担当更重要的角色, 它比传统的字符编码更节省费用,使软件或者网站能够运用于不同的系统平台、语言和国家,而不需要重建,同时也保证了资料在不同系统中的完整性。所以说你只要将中文转换为Unicode,任何国家的人都能看到你想表达的真正意思,而不是乱码。 -
举例
中文原码(GB2312):叁肆伍陆柒捌
转为Unicode后:叁肆伍陆柒捌
网页效果为:叁肆伍陆柒捌
提示:改变网页编码为其它任何国家、语言的编码试试,始终能看见上面红色的Unicode中文。
JS:
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
)mod
7
;
//
移位计数器达7要特殊处理
end;
end;
将中文转为unicode 及转回中文函数(转载)
06.03
.
03
from duduwolf 相关文章(
6
) 以文找文 上一篇 下一篇
Tag:java unicode 中文编程 这篇文章不错,快看看收藏了该文章的所有2人,我也要收藏

//
转为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();
}
/** */
/** 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,提供了JavaScript、Java和JSP等语言实现Unicode转换的示例代码。
3422

被折叠的 条评论
为什么被折叠?



