java本来用的就是unicode编码呀
解决方案 »
- Java 多线程 semaphore的一个问题
- 怎么在txt文件中输入回车符?
- 怎样在jsp中动态显示树装数据
- RMI部署的问题
- 字符串处理
- 提一个笨笨的问题,并预祝大家圣诞快乐
- Runtime.getRuntime().exec("winword.exe") error=2 ???
- 请问如何做流水打印 ?
- byte[] 数组怎么转成 long
- 请问在linux下转化中文问题
- 初学者的问题:目前我应该装那一个java开发工具,jbuilder7? JavaScript?有没有java2.0?现在软件公司开发用的是那一种?
- applet在windowsXP中的IE不能运行,怎么回事?我下载了htmlconvert但不会用呀?
具体说:怎样把GBK编码转换成unicode码?
比如:
GBK: 啊 176161
unicode: 啊 21834
怎样把176161转换成21834?
而且两种编码的排序不同,GBK是按拼音排的,开始字符是“啊”;
而unicode的开始字符则是“一”。
/**
* filename:uI18n.java
* @version 1.0
* @author yakuu
*/package com.yakuu.forum.util;import java.io.*;
import java.io.Serializable;//Convert double byte words to unicode. ignore the character and the numberpublic class uI18n implements java.io.Serializable {
public uI18n(){}
public static String toHEXString(byte b) {
return (""+"0123456789ABCDEF".charAt(0xf&b>>4)+"0123456789ABCDEF".charAt(b&0xF));
}
public static String getUnicode(String setStr) {
String szRet = "";
String szRetTmp = null;
String str = null;
try {
try {
str = new String(setStr.getBytes("ISO-8859-1"),"GBK");
}catch(Exception ex){
ex.printStackTrace();
}
byte[] bb = str.getBytes("UTF16");
for(int i = 2; i < bb.length; i++)
{
i++;
if(bb[i-1] != 0)
szRetTmp = "&#x" + toHEXString(bb[i-1]) + toHEXString(bb[i]) + ";";
else
szRetTmp = new String(bb,i,1,"GBK");
szRet = szRet + szRetTmp;
}
}catch(Exception ex) {
szRet="Sorry Convert2Unicode method fail!";
ex.printStackTrace();
}
return szRet;
}
public static String getUnicode(String setStr,String encoding) {
String str = null;
try {
str = new String(setStr.getBytes(encoding),"ISO-8859-1");
}catch(Exception ex){
ex.printStackTrace();
}
return getUnicode(str);
}
}