The `toRadixString` method in Dart converts this integer to a string representation in the given radix.
The syntax of int.toRadixString() method is:
String toRadixString(int radix)
This toRadixString() method of int converts this to a string representation in the given radix.
Parameter | Optional/Required | Description |
---|---|---|
radix | required | The radix (base) to use for the conversion. |
int.toRadixString() returns value of type String
.
In this example,
num
with the value 255.toRadixString()
method with a radix of 2 to convert it to binary.void main() {
int num = 255;
String binaryString = num.toRadixString(2);
print('Binary representation of $num: $binaryString');
}
Binary representation of 255: 11111111
In this example,
num
with the value 255.toRadixString()
method with a radix of 8 to convert it to octal.void main() {
int num = 255;
String octalString = num.toRadixString(8);
print('Octal representation of $num: $octalString');
}
Octal representation of 255: 377
In this example,
num
with the value 255.toRadixString()
method with a radix of 16 to convert it to hexadecimal.void main() {
int num = 255;
String hexString = num.toRadixString(16);
print('Hexadecimal representation of $num: $hexString');
}
Hexadecimal representation of 255: ff
In this Dart tutorial, we learned about toRadixString() method of int: the syntax and few working examples with output and detailed explanation for each example.