Java中的十六进制转整数

Hexadecimal to Integer in Java(Java中的十六进制转整数)

本文介绍了Java中的十六进制转整数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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();

}

例如,当我传递这个字符串时:_DTOWsHJbEeC6VuzWPawcLA,他的哈希值是:0xC934E5D372B2AB6D0A50B9F0341A00ED029BDC15

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

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

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

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

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!!

感谢您的帮助!!

推荐答案

为什么不使用 java 功能呢:

Why do you not use the java functionality for that:

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

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);  

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

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

  BigInteger value = new BigInteger(hex, 16);

@见JavaDoc:

  • Integer.parseInt(String value, int radix)
  • BigInteger(String value, int radix)

这篇关于Java中的十六进制转整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Java中的十六进制转整数

基础教程推荐