convert a byte array to a hexadecimal string

本文介绍了两种将二进制数据转换为十六进制字符串的方法。一种使用字符数组直接构建十六进制字符串,另一种利用StringBuffer进行逐字节转换,并通过Integer类的静态方法toHexString完成转换。

 public static String ByteArrayToHexString(byte[] bytes) {
     final char[] hexArray = {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

     // Each byte has two hex characters (nibbles)
     char[] hexChars = new char[bytes.length * 2]; 
     int v;
     for (int j = 0; j < bytes.length; j++) {
         // Cast bytes[j] to int, treating as unsigned value
         v = bytes[j] & 0xFF; 
         hexChars[j * 2] = hexArray[v >>> 4]; 
         hexChars[j * 2 + 1] = hexArray[v & 0x0F];
     }
     return new String(hexChars);
 }

或者

/**
 * 二进制转十六进制
 * 因为一个byte对应8位二进制,取值范围是-128~127
 * 所以一个byte对应两个十六进制数字
 * 
 * @param bytes
 * @return
 */
public static String bytesToHex(byte[] bytes) {
    StringBuffer md5str = new StringBuffer();
    //把数组每一字节换成16进制连成md5字符串

    int digital;
    for (int i = 0; i < bytes.length; i++) {
         digital = bytes[i];

        if(digital < 0) {
            digital += 256;
        }
        if(digital < 16){
            md5str.append("0");
        }
        md5str.append(Integer.toHexString(digital));
    }
    return md5str.toString().toUpperCase();
}
import java.io.InputStream; import java.security.DigestInputStream; import java.security.MessageDigest; public class MD5Utils { /** * Used to build output as Hex */ private static final char[] DIGITS_LOWER = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'a', 'b', 'c', 'd', 'e', 'f' }; public static String computeMD5(String input) { if (input == null) { return null; } MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); digest.update(input.getBytes("UTF-8")); } catch (Exception e) { throw new RuntimeException(e.getMessage()); } byte[] digestBytes = digest.digest(); String returnString = new String(encodeHex(digestBytes)); String inputToLog = input; if (inputToLog.length() > 500) { inputToLog = inputToLog.substring(0, 500)+"... [truncated in log]"; } return returnString; } public static String computeMD5(InputStream stream) { MessageDigest digest; try { digest = MessageDigest.getInstance("MD5"); DigestInputStream digestStream = new DigestInputStream(stream, digest); byte[] buf = new byte[20480]; while (digestStream.read(buf) != -1) { ; //digest is updating } } catch (Exception e) { throw new RuntimeException(e); } byte[] digestBytes = digest.digest(); String returnString = new String(encodeHex(digestBytes)); return returnString; } /** * Converts an array of bytes into an array of characters representing the hexadecimal values of each byte in order. * The returned array will be double the length of the passed array, as it takes two characters to represent any * given byte. * * @param data * a byte[] to convert to Hex characters * @return A char[] containing hexadecimal characters */ private static char[] encodeHex(byte[] data) { int l = data.length; char[] out = new char[l << 1]; // two characters form the hex value. for (int i = 0, j = 0; i < l; i++) { out[j++] = DIGITS_LOWER[(0xF0 & data[i]) >>> 4]; out[j++] = DIGITS_LOWER[0x0F & data[i]]; } return out; } } 将这段代码改造成python
09-20
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值