Dart int toRadixString()
Syntax & Examples


int.toRadixString() method

The `toRadixString` method in Dart converts this integer to a string representation in the given radix.


Syntax of int.toRadixString()

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.

Parameters

ParameterOptional/RequiredDescription
radixrequiredThe radix (base) to use for the conversion.

Return Type

int.toRadixString() returns value of type String.



✐ Examples

1 Convert to binary

In this example,

  1. We create an integer variable num with the value 255.
  2. We use the toRadixString() method with a radix of 2 to convert it to binary.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num = 255;
  String binaryString = num.toRadixString(2);
  print('Binary representation of $num: $binaryString');
}

Output

Binary representation of 255: 11111111

2 Convert to octal

In this example,

  1. We create an integer variable num with the value 255.
  2. We use the toRadixString() method with a radix of 8 to convert it to octal.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num = 255;
  String octalString = num.toRadixString(8);
  print('Octal representation of $num: $octalString');
}

Output

Octal representation of 255: 377

3 Convert to hexadecimal

In this example,

  1. We create an integer variable num with the value 255.
  2. We use the toRadixString() method with a radix of 16 to convert it to hexadecimal.
  3. We then print the result to standard output.

Dart Program

void main() {
  int num = 255;
  String hexString = num.toRadixString(16);
  print('Hexadecimal representation of $num: $hexString');
}

Output

Hexadecimal representation of 255: ff

Summary

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.