All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.ruijc.util.encrypt.HexUtils Maven / Gradle / Ivy

package com.ruijc.util.encrypt;

/**
 * 十六进制工具类
 *
 * @author storezhang
 */
public class HexUtils {

    private static final String HEX_CHARS = "0123456789ABCDEF";

    public static String toHexString(byte[] b) {
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < b.length; i++) {
            sb.append(HEX_CHARS.charAt(b[i] >>> 4 & 0x0F));
            sb.append(HEX_CHARS.charAt(b[i] & 0x0F));
        }
        return sb.toString();
    }

    public static byte[] toByteArray(String s) {
        byte[] buf = new byte[s.length() / 2];
        int j = 0;
        for (int i = 0; i < buf.length; i++) {
            buf[i] = (byte) ((Character.digit(s.charAt(j++), 16) << 4) | Character
                    .digit(s.charAt(j++), 16));
        }
        return buf;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy