2

Edit: Resolved the issue with below code:

    String tokenId="0x1800000000001289000000000000000000000000000000000000000000000000";

    BigInteger token1;
    if (tokenId.startsWith("0x")){
        token1=new BigInteger(tokenId.substring(2),16);

I have a long String that I need to assign as BigInteger and pass it to another method of Web3j library. However, I keep receiving number format exception. Any help on this ?

Below is the method throwing exception:

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);
}

Here's the Exception:

java.lang.NumberFormatException: For input string: "0x1800000000001289000000000000000000000000000000000000000000000000" at java.base/java.lang.NumberFormatException.forInputString(NumberFormatException.java:65) at java.base/java.lang.Long.parseLong(Long.java:692) at java.base/java.lang.Long.parseLong(Long.java:817)

Antony Denyer
  • 1,541
  • 1
  • 15
  • 31

3 Answers3

2

Drop the extraneous 0x from the string.

The documentation for BigInteger's constructor says

The String representation consists of an optional minus or plus sign followed by a sequence of one or more digits in the specified radix.

[...] The String may not contain any extraneous characters

No mention of prefixes like 0x (or 0 for octal).

Community
  • 1
  • 1
Federico klez Culloca
  • 26,308
  • 17
  • 56
  • 95
  • The problem is that, if I remove the 0x, it is not identified by the smart contract I am interacting with. And that results in 0 balance from the blockchain, even if there is enough balance. Another limitation is that the smart contract accepts only BigInteger as input. – Gladiator9120 Feb 28 '20 at 09:51
  • A number is a number. If a `BigInteger` is expected, the base doesn't matter. It matters only when talking about strings. – Federico klez Culloca Feb 28 '20 at 09:51
  • Fixed it. Not sure if that's the correct way of doing it or not. But it works for now ! Edited my ques with the solution. Thanks for the help. – Gladiator9120 Feb 28 '20 at 10:01
2

You need to remove the 0x, you can retrieve the hexa value by using toString(16) on your BigInteger

BigInteger tokenId=new BigInteger("1800000000001289000000000000000000000000000000000000000000000000",16);
System.out.println("tokenId.toString(16) = " + tokenId.toString(16));
System.out.println("tokenId.toString(10) = " + tokenId.toString(10));

String originalString = "0x" + tokenId.toString(16);
System.out.println("originalString = " + originalString);

outputs:

tokenId.toString(16) = 1800000000001289000000000000000000000000000000000000000000000000
tokenId.toString(10) = 10855508365998423105807514254364715762064874182780947284375732482585619595264
originalString = 0x1800000000001289000000000000000000000000000000000000000000000000
Bentaye
  • 9,403
  • 5
  • 32
  • 45
0

As you are already using web3j you can use Numeric.decodeQuantity("0x1800000000001289000000000000000000000000000000000000000000000000") this decode the hexideimal into a BigInteger

Antony Denyer
  • 1,541
  • 1
  • 15
  • 31