首页 > 代码库 > android之字符串的一些转码

android之字符串的一些转码

转载自。。。

    ①、将字符串加密称MD5,32位16进制字符串

    ②、将字符串加密称ASCII字串

    ③、将ASCII字串编程16进制字串

import java.security.MessageDigest;public class StringUtils {    public static String replaceUrlWithPlus(String url) {               if (url != null) {            return url.replaceAll("http://(.)*?/", "").replaceAll("[.:/,%?&=]", "+").replaceAll("[+]+", "+");        }        return null;    }        public static String EncodeMD5(String text) throws Exception {        MessageDigest md = MessageDigest.getInstance("MD5");        md.update(text.getBytes("US-ASCII"));        byte[] digest = md.digest();        StringBuffer md5 = new StringBuffer();        for (int i = 0; i < digest.length; i++) {            md5.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));            md5.append(Character.forDigit((digest[i] & 0xF), 16));        }        return md5.toString();    }    public static String EncodeMD5ASCII(String text) throws Exception {        MessageDigest md = MessageDigest.getInstance("MD5");        md.update(text.getBytes("US-ASCII"));        byte[] digest = md.digest();        return new String(digest, "US-ASCII");    }    public static String DecodeMD5Hex(String text) throws Exception {        byte[] digest = text.getBytes();        StringBuffer md5 = new StringBuffer();        for (int i = 0; i < digest.length; i++) {            md5.append(Character.forDigit((digest[i] & 0xF0) >> 4, 16));            md5.append(Character.forDigit((digest[i] & 0xF), 16));        }        return md5.toString();    }}

android之字符串的一些转码