字符串得到其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;
}
/*利用的是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;
}
相关文章推荐
- [C/C++]_[输出内存数据的二进制和十六进制的字符串表示]
- 将二进制数据转换为字符串显示
- 从一副52张的牌中,选出四张,然后计算它们的和。其中,J、Q、K和A分别表示11、12、13和1。编写程序显示得到的和为24的选牌次数,并显示选出的牌。
- 输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n。
- JS(截取字符串,显示当前系统时间yyyy-MM-dd,从文本框得到的数值计算)
- 【codeforces 691 E】【矩阵快速幂 思维题】【给定序列,从序列中选择k个数(可以重复选择),使得得到的排列满足xi与xi+1异或的二进制表示中1的个数是3的倍数。问满足条件的序列个数】
- 计算 24 点是一种扑克牌益智游戏,随机抽出 4 张扑克牌,通过加 (+) ,减 (-) ,乘 ( * ), 除 (/) 四种运算法则计算得到整数 24 ,本问题中,扑克牌通过如下字符或者字符串表示,其中,小写 joker 表示小王,大写 JOKER 表示大王:
- JZTK项目 驾照题库项目servlet层得到的json字符串在浏览器中 汉字部分出现问号?无法正常显示的解决方法
- 得到一个字符串,超过N个汉字或者2*N个英文,之后显示...
- [笔记]一道C语言面试题:得到整数的M进制表示字符串
- 根据字体大小得到字符串显示时的宽度(C#)
- c语言:输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n
- c语言:输入两个整数m和n,计算需要改变m的二进制表示中的多少位才能得到n
- 字符与字符串的表示(ASCII字符,Unicode码,字符串)
- 将一个正整数转化为二进制并用字符串表示输出
- 算法训练 6-2递归求二进制表示位数 ;求完数;求指数;字符串变换
- 数值的八进制、十六进制表示,及以二进制进行显示
- Java Web后台通过request.getParameter( )方法得到的字符串显示为乱
- 022给定一个字符串类型(string)表示的小数,打印出它的二进制表示(keep it up)
- 实验10.3_数值显示拓展_dword型数转变为表示十进制数的字符串