Complete the function which converts hex number (given as a string) to a decimal number.
import "package:test/test.dart"; import "package:solution/solution.dart";
void main() { group("Fixed tests", () { test("Testing for 1", () { expect(hexToDec("1"), equals(1)); }); test("Testing for a", () { expect(hexToDec("a"), equals(10)); }); test("Testing for 10", () { expect(hexToDec("10"), equals(16)); }); test("Testing for FF", () { expect(hexToDec("FF"), equals(255)); }); test("Testing for -C", () { expect(hexToDec("-C"), equals(-12)); }); }); }
내답:
int hexToDec(String hexString) => int.parse(hexString, radix: 16);
int hexToDec(String hexString) => hexString.contains('-')
? int.parse('-' + '0x$hexString'.replaceAll('-', ''))
: int.parse('0x$hexString');