首页 > 移动平台 > 详细

android之字符串的一些转码

时间:2014-09-11 10:57:21      阅读:306      评论:0      收藏:0      [点我收藏+]

转载自。。。

    ①、将字符串加密称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之字符串的一些转码

原文:http://www.cnblogs.com/ccddy/p/3965769.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!