Home > Back-end >  Convert String to BigInteger without Losing Leading Zeroes
Convert String to BigInteger without Losing Leading Zeroes

Time:02-03

Input:

new BigInteger( "0010637234689" );

Output:

10637234689

I am using a Soap Web Service that requires a BigInteger as the ticket number in the request, but it doesn't match it when I don't include the leading zeroes.

Example: If I send an XML from postman with the leading zeroes, I get a success.

<VoidTicketRQ Version="2.1.0" xmlns="http://webservices.sabre.com/sabreXML/2011/10" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
    <Ticketing eTicketNumber="0010637234689"/>
</VoidTicketRQ>

Unfortunately the stubs that are auto-generated by the wsdl files available from that service use a BigInteger, so I cannot populate the payload using a String.

public static class Ticketing
{

    @XmlAttribute(name = "eTicketNumber")
    protected BigInteger eTicketNumber;
}

How can I retain the leading zeroes in a BigInteger?

CodePudding user response:

If the number of digits required are always 13 (quite unlucky), you can send a formatted String:

    BigInteger n = new BigInteger("0010637234689"); // Or "10637234689"
    System.out.printf("%d = 3d%n", n, n);

Will give:

10637234689 = 0010637234689

So use String.format("3d", n).

CodePudding user response:

You can't. You will need to store your "integer" as a String object and convert it as you need to perform you BigInteger operations.

  •  Tags:  
  • Related