Dart BigInt.parse()
Syntax & Examples

BigInt.parse() static-method

The BigInt static parse() method parses the given string source as a, possibly signed, integer literal and returns its value.


Syntax of BigInt.parse()

The syntax of BigInt.parse() static-method is:

 BigInt parse(String source, { int radix }) 

This parse() static-method of BigInt parses source as a, possibly signed, integer literal and returns its value.

Parameters

ParameterOptional/RequiredDescription
sourcerequiredthe string to parse as an integer
radixoptionalthe base of the numeral system used in source (defaults to 10 if not provided)

Return Type

BigInt.parse() returns value of type BigInt.



✐ Examples

1 Parsing a number

In this example,

  1. We create a string numberStr with the value '12345'.
  2. We use the parse() method to parse the string as an integer and store it in a BigInt variable number.
  3. We print the parsed number to standard output.

Dart Program

void main() {
  String numberStr = '12345';
  BigInt number = BigInt.parse(numberStr);
  print('Parsed number: $number');
}

Output

Parsed number: 12345

2 Parsing a character code

In this example,

  1. We create a string charStr with the value 'A'.
  2. We use the parse() method with a radix of 16 (hexadecimal) to parse the string as a hexadecimal integer and store it in a BigInt variable charCode.
  3. We print the parsed character code to standard output.

Dart Program

void main() {
  String charStr = 'A';
  BigInt charCode = BigInt.parse(charStr, radix: 16);
  print('Parsed character code: $charCode');
}

Output

Parsed character code: 10

3 Parsing a binary number

In this example,

  1. We create a string binaryStr with the value '1010'.
  2. We use the parse() method with a radix of 2 (binary) to parse the string as a binary integer and store it in a BigInt variable decimalValue.
  3. We print the parsed binary number to decimal form to standard output.

Dart Program

void main() {
  String binaryStr = '1010';
  BigInt decimalValue = BigInt.parse(binaryStr, radix: 2);
  print('Parsed binary to decimal: $decimalValue');
}

Output

Parsed binary to decimal: 10

Summary

In this Dart tutorial, we learned about parse() static-method of BigInt: the syntax and few working examples with output and detailed explanation for each example.