mirror of
https://github.com/wanghongenpin/proxypin.git
synced 2026-03-15 04:23:17 +08:00
26 lines
694 B
Dart
26 lines
694 B
Dart
//hex ---> int
|
|
int hexToInt(String hex) {
|
|
int val = 0;
|
|
int len = hex.length;
|
|
for (int i = 0; i < len; i++) {
|
|
int hexDigit = hex.codeUnitAt(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 FormatException("Invalid hexadecimal value $hex");
|
|
}
|
|
}
|
|
return val;
|
|
}
|
|
|
|
//int ---> hex
|
|
String intToHex(int i) {
|
|
return i.toRadixString(16).toUpperCase();
|
|
}
|