【转载】java byte转十六进制

public static String bytes2HexString(byte[] b)
{
    String ret = "";
    for (int i = 0; i < b.length; i++)
    {
        String hex = Integer.toHexString(b[i] & 0xFF);
        if (hex.length() == 1)
        {
            hex = '0' + hex;
        }
        ret += hex;
    }
    return ret;
}
public static String toHex(byte[] buffer)
{
    StringBuffer sb = new StringBuffer(buffer.length * 2);
    for (int i = 0; i < buffer.length; i++)
    {
        sb.append(Character.forDigit((buffer[i] & 240) >> 4, 16));
        sb.append(Character.forDigit(buffer[i] & 15, 16));
    }
    return sb.toString();
}

 

posted @ 2013-12-18 15:44  冰鸮  阅读(321)  评论(0编辑  收藏  举报