Logo

Programming-Idioms

  • C++
  • Java
  • Go

Idiom #345 Convert string to big integer

Create the integer value i initialized from its string representation s (in radix 10)

Use an integer type that can hold huge values. Explain what happens if s cannot be parsed.

import "math/big"
i := new(big.Int)
_, ok := i.SetString(s, 10)

If s cannot be parsed, then ok will be false
import java.math.BigInteger;
BigInteger i = new BigInteger(s, 10);
ncalc
i := s;

Most Pascal dialects do not come with a built-in big integer library.
Most "3rd-party" big integer implementations for Pascal (or Delphi) simply overload the assignment operator, as in the example.
The ncalc library raises an exception if s is not a valid big integer.
s may also be a hexadecimal, octal or binary representation.

New implementation...
< >
programming-idioms.org