oracle x4e2d,:怎么把文本里保存的utf8编码&x4E2D;&x6587;读出来转成汉字

Java codepublic class StringToUnicode

{

public static void main(String[] args) throws Exception

{

String str="中文编码";

str=str.replace("",""); //去掉多余的字符。""

str=str.replace(";",""); //去掉";"

String UnicodeString=null;

UnicodeString=stringToUnicode(str);

System.out.println(UnicodeString);

}

//把字符串转换成Unicode字符串,输入的字符串中只能是0-9数字或者是A--F字母,不能有任何其他字符

//输入字符串要是偶数个字符。

public static String stringToUnicode(String str) throws Exception

{

byte[] bytes=new byte[str.length()/2+2]; //定义字节数组,长度为字符串的一半。加 2 是存放unicode 编码头(ff,fe)

bytes[0]=-2; //-2 对应fe,-1对应ff. 后面要交换,所以保存 fe,ff.

bytes[1]=-1;

byte tempByte=0; //临时变量。

byte tempHigh=0;

byte tempLow=0;

for(int i=0,j=2;i

{

tempByte=(byte)(((int)str.charAt(i))&0xff); //处理高位。

if(tempByte>=48&&tempByte<=57)

{

tempHigh=(byte)((tempByte-48)<<4); //'0'对应48。

}

else if(tempByte>=65&&tempByte<=70) //'A'--'F'

{

tempHigh=(byte)((tempByte-65+10)<<4);

}

tempByte=(byte)(((int)str.charAt(i+1))&0xff); //处理低位。

if(tempByte>=48&&tempByte<=57)

{

tempLow=(byte)(tempByte-48);

}

else if(tempByte>=65&&tempByte<=70) //'A'--'F'

{

tempLow=(byte)(tempByte-65+10); //'A'对应10.(或0xa.)

}

bytes[j]=(byte)(tempHigh|tempLow); //通过‘或’加在一起。

}

for(int i=0;i

{

byte b1=bytes[i];

bytes[i]=bytes[i+1];

bytes[i+1]=b1;

}

String result=new String(bytes,"Unicode");

return result;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值