BigInteger引发numberFormatException

问题描述 投票:0回答:1

我有一个长字符串,需要将其分配为BigInteger并将其传递给Web3j库的另一种方法。但是,我一直收到数字格式异常。任何帮助吗?

下面是方法抛出异常:

public void getBalance1155(String walletAddress) throws ExecutionException, InterruptedException {

    //define constant values

    Web3j web3j=Web3j.build(new HttpService("https://mainnet.infura.io/v3/<apiKey>>"));
    String contractAddress = "0xfaaFDc07907ff5120a76b34b731b278c38d6043C";
    BigInteger tokenId=new BigInteger("0x1800000000001289000000000000000000000000000000000000000000000000",16);
    NoOpProcessor processor = new NoOpProcessor(web3j);
    Credentials credentials = Credentials.create("privatekey");
    TransactionManager txManager = new FastRawTransactionManager(web3j, credentials, processor);

    //Query Blockchain to get balance of WALLETADDRESS from Contract for given TokenID

    ERC1155 token = ERC1155.load(contractAddress, web3j, txManager, DefaultGasProvider.GAS_PRICE, DefaultGasProvider.GAS_LIMIT);
    RemoteCall<BigInteger> sendCall = token.balanceOf(walletAddress, tokenId);
    BigInteger balance=sendCall.sendAsync().get();
    log.info("balance >>>>>> " +balance);
}

这里是例外:

java.lang.NumberFormatException:对于输入字符串:“ 0x1800000000001289000000000000000000000000000000000000000000000000000000000000”在java.base / java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)在java.base / java.lang.Long.parseLong(Long.java:692)在java.base / java.lang.Long.parseLong(Long.java:817)

java biginteger
1个回答
1
投票

从字符串中删除无关的0x

BigInteger's constructor的文档说

字符串表示形式由一个可选的减号或加号组成,后跟指定基数中的一个或多个数字序列。

[...]字符串不能包含任何多余的字符

没有提及像BigInteger(或0x代表八进制)的前缀。


0
投票

您需要删除0BigInteger tokenId = new BigInteger(“ 1800000000001289000000000000000000000000000000000000000000000000000000000000”,16);System.out.println(“ tokenId base 16 =” + tokenId.toString(16));System.out.println(“ tokenId base 10 =” + tokenId.toString(10));

输出:

0x
© www.soinside.com 2019 - 2024. All rights reserved.