Dart: convert decimal to hex

Here is a little fuller example:

final myInteger = 2020;
final hexString = myInteger.toRadixString(16);      // 7e4

The radix just means the base, so 16 means base-16. You can use the same method to make a binary string:

final binaryString = myInteger.toRadixString(2);    // 11111100100

If you want the hex string to always be four characters long then you can pad the left side with zeros:

final paddedString = hexString.padLeft(4, '0');     // 07e4

And if you prefer it in uppercase hex:

final uppercaseString = paddedString.toUpperCase(); // 07E4

Here are a couple other interesting things:

print(0x7e4); // 2020

int myInt = int.parse('07e4', radix: 16);
print(myInt); // 2020

 int.toRadixString(16) 

does that.

See also https://groups.google.com/a/dartlang.org/forum/m/#!topic/misc/ljkYEzveYWk

Tags:

Dart