Convert hex string to int should exit. #2624
Labels
area-core-library
SDK core library issues (core, async, ...); use area-vm or area-web for platform specific libraries.
type-enhancement
A request for a change that isn't a bug
Following function should exist in Dart's built in library. Currently, it's implement in the lib/ui style.dart file.
/**
* [hex] hexidecimal string to convert to scalar.
* returns hexidecimal number as an integer.
* throws BadNumberFormatException if [hex] isn't a valid hex number.
*/
static int hexToNum(String hex) {
int val = 0;
int len = hex.length;
for (int i = 0; i < len; i++) {
int hexDigit = hex.charCodeAt(i);
if (hexDigit >= 48 && hexDigit <= 57) {
val += (hexDigit - 48) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 65 && hexDigit <= 70) {
// A..F
val += (hexDigit - 55) * (1 << (4 * (len - 1 - i)));
} else if (hexDigit >= 97 && hexDigit <= 102) {
// a..f
val += (hexDigit - 87) * (1 << (4 * (len - 1 - i)));
} else {
throw new BadNumberFormatException("Bad hexidecimal value");
}
}
return val;
}
The text was updated successfully, but these errors were encountered: