【问题标题】:How can I convert this large number hex value to decimal如何将这个大的十六进制值转换为十进制
【发布时间】:2023-02-20 21:50:40
【问题描述】:

我正在尝试将字符串 ABCDEF1234567890 转换为十进制值:

    long result = 0;
    String hex = "0123456789ABCDEF";
    decimal = decimal.toUpperCase();
    for(int i = 0; i < decimal.length(); i++) {
        char c = decimal.charAt(i);
        result += hex.indexOf(c) * Math.pow(16, decimal.length() - 1 - i);
    }
    return Long.toString(result);

我知道 BigInteger 类,但我不知道如何在我的代码中使用它。请帮我

【问题讨论】:

  • BigInteger 是去这里的路。你读过课程的文档了吗?
  • 我不知道如何在我的代码中使用它。你能帮助我吗
  • 你可以从BigInteger result = BigInteger.ZERO;开始。那就看看result有哪些方法吧。
  • new BigInteger(decimal, 16)

标签: java hex decimal biginteger


【解决方案1】:

BigInteger 类有一个 constructor 接受字符串和基数:

final BigInteger bigInt = new BigInteger(hex, 16);

请注意,您的特定十六进制值 0123456789ABCDEF 适合类型为 long (Long.MAX_VALUE == 0x7fffffffffffffffL) 的变量,并且 Long#parseLong 也接受字符串和基数:

final long value = Long.parseLong(hex);

【讨论】:

    猜你喜欢
    • 2014-02-05
    • 1970-01-01
    • 1970-01-01
    • 2015-09-13
    • 2013-10-08
    • 2011-07-28
    • 1970-01-01
    相关资源
    最近更新 更多