简体   繁体   中英

How can I convert a large string to integer in java?

Given the following string:

3132333435363738396162636465666768696a6b6c6d6e6f70

I converted the string to hex and now i want to file write it as hex not a string. I tried converting it to int but Integer.parseInt converts up to 4 only and if go beyond that it would give error already.

Have you tried the BigInteger constructor taking a string and a radix ?

BigInteger value = new BigInteger(hex, 16);

Sample code:

import java.math.BigInteger;

public class Test {

    public static void main(String[] args) {
        String hex = "3132333435363738396162636465666768696a6b6c6d6e6f70";
        BigInteger number = new BigInteger(hex , 16);
        System.out.println(number); // As decimal...
    }
}

Output:

308808885829455478403317837970537433512288994552567292653424

使用BigInteger ,其中一个构造函数采用基数。

BigInteger value = new BigInteger(hexString, 16);

尝试:

new BigInteger("3132333435363738396162636465666768696a6b6c6d6e6f70", 16)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM