Java 中的十六进制到整数

发布于 2024-11-05 05:47:58 字数 1416 浏览 1 评论 0原文

我正在尝试将字符串十六进制转换为整数。十六进制字符串是根据哈希函数 (sha-1) 计算得出的。我收到此错误:java.lang.NumberFormatException。我猜它不喜欢十六进制的字符串表示形式。我怎样才能做到这一点。这是我的代码:

public Integer calculateHash(String uuid) {

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        digest.update(uuid.getBytes());
        byte[] output = digest.digest();

        String hex = hexToString(output);
        Integer i = Integer.parseInt(hex,16);
        return i;           

    } catch (NoSuchAlgorithmException e) {
        System.out.println("SHA1 not implemented in this system");
    }

    return null;
}   

private String hexToString(byte[] output) {
    char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F' };
    StringBuffer buf = new StringBuffer();
    for (int j = 0; j < output.length; j++) {
        buf.append(hexDigit[(output[j] >> 4) & 0x0f]);
        buf.append(hexDigit[output[j] & 0x0f]);
    }
    return buf.toString();

}

例如,当我传递此字符串:_DTOWsHJbEeC6VuzWPawcLA时,他的散列是:0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15

但我得到:java.lang.NumberFormatException:对于输入字符串:“ 0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15

我真的需要这样做。我有一个由 UUID 标识的元素集合,这些元素是字符串。我必须存储这些元素,但我的限制是使用整数作为它们的 id。这就是为什么我计算给定参数的哈希值,然后转换为 int。也许我做错了,但有人可以给我建议以正确实现这一目标!

感谢您的帮助 !!

I am trying to convert a String hexadecimal to an integer. The string hexadecimal was calculated from a hash function (sha-1). I get this error : java.lang.NumberFormatException. I guess it doesn't like the String representation of the hexadecimal. How can I achieve that. Here is my code :

public Integer calculateHash(String uuid) {

    try {
        MessageDigest digest = MessageDigest.getInstance("SHA1");
        digest.update(uuid.getBytes());
        byte[] output = digest.digest();

        String hex = hexToString(output);
        Integer i = Integer.parseInt(hex,16);
        return i;           

    } catch (NoSuchAlgorithmException e) {
        System.out.println("SHA1 not implemented in this system");
    }

    return null;
}   

private String hexToString(byte[] output) {
    char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
            'A', 'B', 'C', 'D', 'E', 'F' };
    StringBuffer buf = new StringBuffer();
    for (int j = 0; j < output.length; j++) {
        buf.append(hexDigit[(output[j] >> 4) & 0x0f]);
        buf.append(hexDigit[output[j] & 0x0f]);
    }
    return buf.toString();

}

For example, when I pass this string : _DTOWsHJbEeC6VuzWPawcLA, his hash his : 0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15

But i get : java.lang.NumberFormatException: For input string: "0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15"

I really need to do this. I have a collection of elements identified by their UUID which are string. I will have to store those elements but my restrictions is to use an integer as their id. It is why I calculate the hash of the parameter given and then I convert to an int. Maybe I am doing this wrong but can someone gives me an advice to achieve that correctly!!

Thanks for your help !!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

滥情稳全场 2024-11-12 05:47:58

为什么不使用 java 功能:

如果您的数字很小(比您的小),您可以使用:Integer.parseInt(hex, 16) 将十六进制字符串转换为整数。

  String hex = "ff"
  int value = Integer.parseInt(hex, 16);  

对于像您这样的大数字,请使用 public BigInteger(String val, int radix)

  BigInteger value = new BigInteger(hex, 16);

@See JavaDoc:

Why do you not use the java functionality for that:

If your numbers are small (smaller than yours) you could use: Integer.parseInt(hex, 16) to convert a Hex - String into an integer.

  String hex = "ff"
  int value = Integer.parseInt(hex, 16);  

For big numbers like yours, use public BigInteger(String val, int radix)

  BigInteger value = new BigInteger(hex, 16);

@See JavaDoc:

吾家有女初长成 2024-11-12 05:47:58

试试这个

public static long Hextonumber(String hexval)
    {
        hexval="0x"+hexval;
//      String decimal="0x00000bb9";
        Long number = Long.decode(hexval);
//.......       System.out.println("String [" + hexval + "] = " + number);
        return number;
        //3001
    }

Try this

public static long Hextonumber(String hexval)
    {
        hexval="0x"+hexval;
//      String decimal="0x00000bb9";
        Long number = Long.decode(hexval);
//.......       System.out.println("String [" + hexval + "] = " + number);
        return number;
        //3001
    }
柏林苍穹下 2024-11-12 05:47:58

我终于根据您的所有评论找到了我的问题的答案。谢谢,我尝试了这个:

public Integer calculateHash(String uuid) {

    try {
        //....
        String hex = hexToString(output);
        //Integer i = Integer.valueOf(hex, 16).intValue();
        //Instead of using Integer, I used BigInteger and I returned the int value.
        BigInteger bi = new BigInteger(hex, 16);
        return bi.intValue();`
    } catch (NoSuchAlgorithmException e) {
        System.out.println("SHA1 not implemented in this system");
    }
    //....
}

这个解决方案不是最佳的,但我可以继续我的项目。再次感谢您的帮助

I finally find answers to my question based on all of your comments. Thanks, I tried this :

public Integer calculateHash(String uuid) {

    try {
        //....
        String hex = hexToString(output);
        //Integer i = Integer.valueOf(hex, 16).intValue();
        //Instead of using Integer, I used BigInteger and I returned the int value.
        BigInteger bi = new BigInteger(hex, 16);
        return bi.intValue();`
    } catch (NoSuchAlgorithmException e) {
        System.out.println("SHA1 not implemented in this system");
    }
    //....
}

This solution is not optimal but I can continue with my project. Thanks again for your help

墟烟 2024-11-12 05:47:58

SHA-1 生成 160 位消息(20 字节),太大而无法存储在 intlong 值中。正如 Ralph 所建议的,您可以使用 BigInteger。

要获得(不太安全的)int 哈希,您可以返回返回的字节数组的哈希代码。

或者,如果您根本不需要 SHA,您可以只使用 UUID 的字符串哈希码。

SHA-1 produces a 160-bit message (20 bytes), too large to be stored in an int or long value. As Ralph suggests, you could use BigInteger.

To get a (less-secure) int hash, you could return the hash code of the returned byte array.

Alternatively, if you don't really need SHA at all, you could just use the UUID's String hash code.

忘羡 2024-11-12 05:47:58

您可以使用此方法:https://stackoverflow.com/a/31804061/3343174
它将任何十六进制数(以字符串形式表示)完美转换为十进制数

you can use this method : https://stackoverflow.com/a/31804061/3343174
it's converting perfectly any hexadecimal number (presented as a string) to a decimal number

甜扑 2024-11-12 05:47:58

这是因为 byte[] 输出 很好,并且是字节数组,您可能会将其视为表示每个整数的字节数组,但是当您将它们全部添加到单个字符串中时,您会得到不是整数的东西,这就是原因。您可以将其作为整数数组,也可以尝试创建 BigInteger

That's because the byte[] output is well, and array of bytes, you may think on it as an array of bytes representing each one an integer, but when you add them all into a single string you get something that is NOT an integer, that's why. You may either have it as an array of integers or try to create an instance of BigInteger.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文