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



/*利用的是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;

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值