{

How to convert decimal to/from a hexadecimal number in Dart


convert decimal to/from a Hexa number in Dart and Flutter
  • Convert Hexa to Decimal in Dart:

    • using int.parseInt(String, {radix: base}) method, base as 16 .int.parse('0009e', radix: 2); returns decimal value 158.
  • Convert Decimal to Hexa in Dart:

    • use Number.toRadixString(16) method, For example: 158.toRadixString(16) returns hexa value 9e.

A Hexadecimal number often known as a Hexa number, is a 16-digit number. It is based on the Base 16 numbering system, also known as hexadecimal.

In Dart, hexadecimal numerals include decimal numbers (0-9) and six alphabets ranging from A to F, i.e. 0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F. 42E or 4C,4B, are hexadecimal numbers examples.

Numbers with a base of ten, or numbers ranging from 0 to 9, are known as ‘Decimal Numbers.’

45 is an example of a decimal number. Learn how to convert decimal to hexadecimal in a dart in this quick tutorial.

How to convert decimal numbers to hexadecimal numbers in Dart?

In this example, a Decimal number with base 10 is converted to a Hexa number with base 16 in dart and flutter

decimal numbers are represented using int type in dart. It has the toRadixString() method which accepts the base of an argument.

Number.toRadixString(base)

the base can be 16 for Hexa, 8 for octal,2 for binary.

Here is a code for parsing decimal to hexadecimal example.

void main() {
  final decimalNumber = 158;
  final hexaNumber = decimalNumber.toRadixString(16); // 9e
  print(hexaNumber);
  final fiveDigitHexaNumber = hexaNumber.padLeft(5, '0');
  print(fiveDigitHexaNumber);

  int decimalNumber1 = int.parse('9e', radix: 16);
  print(decimalNumber1);
}

Output:

9e
0009e

if Hexa number always wants to be 5 digits use the padLeft method as given below.

  final fiveDigitHexaNumber = hexaNumber.padLeft(5, '0');

It appends left padding with zeroes.

How to parse hexadecimal numbers to decimal numbers in Dart?

This example converts Hexa to decimal numbers in dart and flutter.

The int has the parseInt method which takes string number and base

syntax

int.parseInt(String, {radix: base})

The string is a string of hexadecimal numbers to convert. the base is a base such as 2, 8,16, etc.

Here is a code for convert hexadecimal to decimal example

void main() {
  final decimalNumber = 158;
  final hexaNumber = decimalNumber.toRadixString(16);
  print(decimalNumber.runtimeType);
  print(hexaNumber);
  final fiveDigitHexaNumber = hexaNumber.padLeft(5, '0');
  print(fiveDigitHexaNumber);

  int decimalNumber1 = int.parse('0009e', radix: 16);
  print(decimalNumber1);
}

Output:

9e
0009e
158

Conclusion

In a summary, Learned the following examples

  • Convert decimal to hexadecimal number using the toRadixString method
  • Convert hexadecimal to decimal number using parseInt() method
THE BEST NEWSLETTER ANYWHERE
Join 6,000 subscribers and get a daily digest of full stack tutorials delivered to your inbox directly.No spam ever. Unsubscribe any time.

Similar Posts
Subscribe
You'll get a notification every time a post gets published here.





Related posts

Perl How to: Find a Given Variable String is numeric or not

Dart/Flutter: Check if String is Empty, Null, or Blank example

How to generate Unique Id UUID in Dart or Flutter Programming| Dart or Flutterby Example

How to Sort List of numbers or String in ascending and descending in Dart or Flutter example

Dart Enum comparison operator| Enum compareByIndex example| Flutter By Example

Dart Example - Program to check Leap year or not

Dart Example to count the number of digits in a number| Decimals Count in Dart