如何将c toHex方法转换为java

时间:2016-02-17 08:19:43

标签: java android c

有谁知道如何将打击代码段转换为java代码? 感谢。

void
rawdata_to_hex (const unsigned char *rawdata, char *hex_str, int n_bytes)
{
    static const char hex[] = "0123456789abcdef";
    int i;

    for (i = 0; i < n_bytes; i++) {
        unsigned int val = *rawdata++;
        *hex_str++ = hex[val >> 4];
        *hex_str++ = hex[val & 0xf];
    }
    *hex_str = '\0';
}

我使用下面的代码,但看起来不正确。

public static String toHex(byte[] buf) {
        if (buf == null) return "";
        StringBuffer result = new StringBuffer(2 * buf.length);
        for (int i = 0; i < buf.length; i++) {
            appendHex(result, buf[i]);
        }
        return result.toString();
    }

    private final static String HEX = "0123456789abcdef";
    private static void appendHex(StringBuffer sb, byte b) {
        sb.append(HEX.charAt((b >> 4) & 0x0f)).append(HEX.charAt(b & 0x0f));
    }

我的问题是如何隐蔽?有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

使用以下方法对我有用

Private Sub MyDataGridView1_CellMouseClick(sender As Object, e As DataGridViewCellMouseEventArgs) Handles MyDataGridView1.CellMouseClick
        RadioButton3.Checked = True

    End Sub

或其他方式

/**
 * Convert byte to Hexadecimal
 *
 * @param bytes
 * @return
 */
private static String toHex(byte[] bytes) throws NoSuchAlgorithmException {
    BigInteger bi = new BigInteger(1, bytes);
    String hex = bi.toString(16);
    int paddingLength = (bytes.length * 2) - hex.length();
    if (paddingLength > 0) {
        return String.format("%0" + paddingLength + "d", 0) + hex;
    } else {
        return hex;
    }
}