网上有一段Unicode转UTF8的,如下所示,我希望寻一段代码像下面将Unicode转成GBK: public static byte[] convertUnicode2UTF8Byte(String s)
{
{
byte b[];
byte b1[];
int len;
int count=0;
char c;
if(s==null || s.length()==0)
return null;
len=s.length();
b=new byte[len*3];
for(int i=0;i <len;i++)
{
c=s.charAt(i);
if(0x0080 > c)
{
b[count++]=(byte)c;
}
else if(0x0800 > c)
{
b[count++]=(byte)((c >> 6)| 0xc0);
b[count++]=(byte)((c & 0x003F) | 0x80);
}
else
{
b[count++]=(byte)((c >> 12) | 0xE0);
b[count++]=(byte)(((c & 0x0FC0)>> 6) | 0x80);
b[count++]=(byte)((c & 0x003F) | 0x80);
}
}
b1=new byte[count+1];
System.arraycopy(b,0,b1,0,count+1);
b=null;
return b1;
}
}
{
{
byte b[];
byte b1[];
int len;
int count=0;
char c;
if(s==null || s.length()==0)
return null;
len=s.length();
b=new byte[len*3];
for(int i=0;i <len;i++)
{
c=s.charAt(i);
if(0x0080 > c)
{
b[count++]=(byte)c;
}
else if(0x0800 > c)
{
b[count++]=(byte)((c >> 6)| 0xc0);
b[count++]=(byte)((c & 0x003F) | 0x80);
}
else
{
b[count++]=(byte)((c >> 12) | 0xE0);
b[count++]=(byte)(((c & 0x0FC0)>> 6) | 0x80);
b[count++]=(byte)((c & 0x003F) | 0x80);
}
}
b1=new byte[count+1];
System.arraycopy(b,0,b1,0,count+1);
b=null;
return b1;
}
}
解决方案 »
- 时间格式化问题。。。。急
- 一个Sqlserver的问题
- 高分求服务器模拟解决方案!请大家给些意见,言者有分!
- 键盘事件问题!
- 如何把4个字节的INT型转换成字节数组byte []?
- (在线等待)安装:Oracle9i Application Server 点击SETUP文件没有反应???
- Ant的HelloWorld(Ant入门必读)
- 怎样设置按钮,使按钮不是突起的(看不到按钮边框)?
- /** 急!中文乱码的一些问题 */
- public成员为什么找不到?头大啊……
- 一个文本编辑器的问题,里面有设置部分文本的颜色字体这些功能
- 在多线程中是否碰过需要System.out.println()打印点东西程序线程才会跑顺畅的?
记得有看到介绍,GBK编码的时候为了兼容其他编码,是从别的编码中,整区位移出来的.
如:楼主的那个convertUnicode2UTF8Byte是多此一举的,,
用byte[] b=unicodeStr.getBytes("UTF-8");就行了。不知楼主为何要多此一举。[楼主可以比较两者之值]
另外。将UNICODE串转换成GBK字节数组用:
byte[] b=unicodeStr.getBytes("GBK");就行了楼主把问题想得复杂了。
OuputStreamReader(outputStrea,"gbk");
e186. Converting Between Strings (Unicode) and Other Character Set Encodings
String newstr = new String(oldstr.getBytes("unicode"),"GBK");
不行吗?