by @kodeazy
flutter how to convert binary to decimal?
- In this blog we will discuss on how to convert binary to decimal number in flutter.
1 | 0 | 1 | 0 | 1 |
---|---|---|---|---|
24 | 23 | 22 | 21 | 20 |
(10101)2 = (1×24)+(0×23)+(1×22)+(0×21)+(1×20)
= 2110
Below is an example program using predefined functions
void main() {
String binaryInput = "1010";
print(int.tryParse("1010", radix: 2) ?? 0);
}
Output
10
Below is an example program without using predefined functions
void main() {
String binaryInput = "1010";
int decimal = 0;
for (int i = 0; i < binaryInput.length; i++) {
if (binaryInput[binaryInput.length - i - 1] == '1') {
decimal += (1 << i);
}
}
print("Decimal value of 1010 is $decimal");
}
Output
Decimal value of 1010 is 10