Ethan Miller
Ethan Miller

Reputation: 571

Convert String to BigInteger without Losing Leading Zeroes

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?

Upvotes: 1

Views: 671

Answers (2)

Joop Eggen
Joop Eggen

Reputation: 109593

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 = %013d%n", n, n);

Will give:

10637234689 = 0010637234689

So use String.format("%013d", n).

Upvotes: 2

Pablo Santa Cruz
Pablo Santa Cruz

Reputation: 181350

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.

Upvotes: 0

Related Questions