首页 > 系统 > Android > 正文

Android字符串转Ascii码实例代码

2020-04-11 11:59:19
字体:
来源:转载
供稿:网友

复制代码 代码如下:

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));
    }
}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表