您的位置:首页 > 其它

字符串得到其unicode码的二进制表示(字符串显示),反过来亦可

2013-11-21 22:07 344 查看

/*利用的是java下,一个字符占两个字节,unicode码也是两个字节*/

public String strtoerjz(String str){//从字符串str转换到其unicode码下二进制表示的字符串erjz

short temp;

byte shiliu=15;

String erjz="";

for(int i=0;i<str.length();i++){

temp=(short)str.charAt(i);

while(shiliu>=0){

if(Math.pow(2,shiliu)<temp){

erjz+='1';

temp-=Math.pow(2, shiliu);

shiliu--;

}

else if(Math.pow(2, shiliu)>temp){

erjz+='0';

shiliu--;

}

else{

erjz+='1';

shiliu--;

while(shiliu>0){

erjz+='0';

shiliu--;

}

}

}

shiliu=15;

}

return erjz;

}

public String erjztostr(byte[] byt){//byt数组保存的是0或1的字符串unicode码,byt[0]=1/0.....返回str其对应的unicode码下的值

String str="";short temp=0;

for(int i=0,num=0;i<byt.length;i++){

temp+=Math.pow(2,15-i+num)*byt[i];

if((i+1)%16==0){

str+=(char)temp;

temp=0;

num+=16;

}

}

return str;

}

public String erjztostR(String str){//str是0或1的字符串的unicode码,返回endstr是其对应的unicode码下的值

String endstr="";short temp=0;

for(int i=0;i<str.length();i++){

temp+=Math.pow(2,15-i+num)*(str.charAt(i)-'0');

if(i+1)%16==0){

endstr+=(char)temp;

temp=0;

num+=16;

}

}

return endstr;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐