Java中的十六进制整数 [英] Hexadecimal to Integer in Java

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

问题描述

我试图将一个十六进制字符串转换为一个整数。字符串十六进制是根据散列函数(sha-1)计算得出的。我得到这个错误:java.lang.NumberFormatException。我想它不喜欢十六进制的字符串表示。我怎样才能做到这一点。这是我的代码:

  public Integer calculateHash(String uuid){

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

字符串十六进制= hexToString(输出);
Integer i = Integer.parseInt(hex,16);
返回i;

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

返回null;

$ b 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 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。也许我这样做是错误的,但有人能给我一个建议,以实现这一点正确!!

感谢您的帮助!!



如果你的数字很小(小于你的数字) )可以使用: Integer.parseInt(hex,16)将Hex - 字符串转换为整数。

 字符串十六进制=ff
int值= Integer.parseInt(十六进制,16);

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

  BigInteger value = new BigInteger(hex,16); 

@See JavaDoc:


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

解决方案

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:

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆