• <small id='RtjvT'></small><noframes id='RtjvT'>

      <i id='RtjvT'><tr id='RtjvT'><dt id='RtjvT'><q id='RtjvT'><span id='RtjvT'><b id='RtjvT'><form id='RtjvT'><ins id='RtjvT'></ins><ul id='RtjvT'></ul><sub id='RtjvT'></sub></form><legend id='RtjvT'></legend><bdo id='RtjvT'><pre id='RtjvT'><center id='RtjvT'></center></pre></bdo></b><th id='RtjvT'></th></span></q></dt></tr></i><div id='RtjvT'><tfoot id='RtjvT'></tfoot><dl id='RtjvT'><fieldset id='RtjvT'></fieldset></dl></div>

    1. <legend id='RtjvT'><style id='RtjvT'><dir id='RtjvT'><q id='RtjvT'></q></dir></style></legend>

        <tfoot id='RtjvT'></tfoot>

          <bdo id='RtjvT'></bdo><ul id='RtjvT'></ul>

        Java中的十六进制转整数

        时间:2023-10-01
            <tbody id='X0HLP'></tbody>

            <legend id='X0HLP'><style id='X0HLP'><dir id='X0HLP'><q id='X0HLP'></q></dir></style></legend>
          1. <tfoot id='X0HLP'></tfoot>
          2. <i id='X0HLP'><tr id='X0HLP'><dt id='X0HLP'><q id='X0HLP'><span id='X0HLP'><b id='X0HLP'><form id='X0HLP'><ins id='X0HLP'></ins><ul id='X0HLP'></ul><sub id='X0HLP'></sub></form><legend id='X0HLP'></legend><bdo id='X0HLP'><pre id='X0HLP'><center id='X0HLP'></center></pre></bdo></b><th id='X0HLP'></th></span></q></dt></tr></i><div id='X0HLP'><tfoot id='X0HLP'></tfoot><dl id='X0HLP'><fieldset id='X0HLP'></fieldset></dl></div>
              <bdo id='X0HLP'></bdo><ul id='X0HLP'></ul>

              • <small id='X0HLP'></small><noframes id='X0HLP'>

                • 本文介绍了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中的十六进制转整数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:为什么 int num = Integer.getInteger("123") 会抛出 下一篇:为什么 == 与 Integer.valueOf(String) 的比较对 127 和

                  相关文章

                  最新文章

                • <legend id='QpzCd'><style id='QpzCd'><dir id='QpzCd'><q id='QpzCd'></q></dir></style></legend>
                    <i id='QpzCd'><tr id='QpzCd'><dt id='QpzCd'><q id='QpzCd'><span id='QpzCd'><b id='QpzCd'><form id='QpzCd'><ins id='QpzCd'></ins><ul id='QpzCd'></ul><sub id='QpzCd'></sub></form><legend id='QpzCd'></legend><bdo id='QpzCd'><pre id='QpzCd'><center id='QpzCd'></center></pre></bdo></b><th id='QpzCd'></th></span></q></dt></tr></i><div id='QpzCd'><tfoot id='QpzCd'></tfoot><dl id='QpzCd'><fieldset id='QpzCd'></fieldset></dl></div>

                    <small id='QpzCd'></small><noframes id='QpzCd'>

                    <tfoot id='QpzCd'></tfoot>

                    • <bdo id='QpzCd'></bdo><ul id='QpzCd'></ul>