Android字符串转Ascii码实例代码
程序员文章站
2022-07-11 19:35:38
复制代码 代码如下:package com.my.page;public class stringtoascii { private s...
复制代码 代码如下:
package com.my.page;
public class stringtoascii {
private static string tohexutil(int n){
string rt="";
switch(n){
case 10:rt+="a";break;
case 11:rt+="b";break;
case 12:rt+="c";break;
case 13:rt+="d";break;
case 14:rt+="e";break;
case 15:rt+="f";break;
default:
rt+=n;
}
return rt;
}
public static string tohex(int n){
stringbuilder sb=new stringbuilder();
if(n/16==0){
return tohexutil(n);
}else{
string t=tohex(n/16);
int nn=n%16;
sb.append(t).append(tohexutil(nn));
}
return sb.tostring();
}
public static string parseascii(string str){
stringbuilder sb=new stringbuilder();
byte[] bs=str.getbytes();
for(int i=0;i<bs.length;i++)
sb.append(tohex(bs[i]));
return sb.tostring();
}
public static void main(string args[]){
string s="xyz";
system.out.println("转换后的字符串是:"+stringtoascii.parseascii(s));
}
}
下一篇: 黑枸杞和茶能够一起泡吗