Dart BigInt toRadixString()
Syntax & Examples
BigInt.toRadixString() method
The `toRadixString` method of BigInt class in Dart converts a BigInt to a string representation in a specified radix.
Syntax of BigInt.toRadixString()
The syntax of BigInt.toRadixString() method is:
String toRadixString(int radix)
This toRadixString() method of BigInt converts this to a string representation in the given radix
.
Parameters
Parameter | Optional/Required | Description |
---|---|---|
radix | required | the radix (base) for the string representation |
Return Type
BigInt.toRadixString() returns value of type String
.
✐ Examples
1 Convert to Binary
In this example,
- We create a BigInt object,
num
, initialized with 255. - We use the
toRadixString
method with a radix of 2 to convert the BigInt to binary representation. - We print the binary representation to standard output.
Dart Program
void main() {
BigInt num = BigInt.from(255);
String binary = num.toRadixString(2);
print('Binary representation: $binary');
}
Output
Binary representation: 11111111
2 Convert to Hexadecimal
In this example,
- We create a BigInt object,
num
, initialized with 12345. - We use the
toRadixString
method with a radix of 16 to convert the BigInt to hexadecimal representation. - We print the hexadecimal representation to standard output.
Dart Program
void main() {
BigInt num = BigInt.from(12345);
String hexadecimal = num.toRadixString(16);
print('Hexadecimal representation: $hexadecimal');
}
Output
Hexadecimal representation: 3039
3 Convert to Octal
In this example,
- We create a BigInt object,
num
, initialized with 98765. - We use the
toRadixString
method with a radix of 8 to convert the BigInt to octal representation. - We print the octal representation to standard output.
Dart Program
void main() {
BigInt num = BigInt.from(98765);
String octal = num.toRadixString(8);
print('Octal representation: $octal');
}
Output
Octal representation: 300715
Summary
In this Dart tutorial, we learned about toRadixString() method of BigInt: the syntax and few working examples with output and detailed explanation for each example.