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
| Parameter | Optional/Required | Description |
|---|---|---|
source | required | the string to parse as an integer |
radix | optional | the 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,
- We create a string
numberStrwith the value '12345'. - We use the
parse()method to parse the string as an integer and store it in a BigInt variablenumber. - 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,
- We create a string
charStrwith the value 'A'. - 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 variablecharCode. - 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,
- We create a string
binaryStrwith the value '1010'. - 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 variabledecimalValue. - 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.