I need to convert a hex string to a decimal value. I have used the following method. But sometimes it returns the wrong decimal value.
Hex string is in this format "00 00 0C 6E"
unsigned long hexToDec(String hexString) {
unsigned long decValue = 0;
int nextInt;
for (long i = 0; i < hexString.length(); i++) {
nextInt = long(hexString.charAt(i));
if (nextInt >= 48 && nextInt <= 57) nextInt = map(nextInt, 48, 57, 0, 9);
if (nextInt >= 65 && nextInt <= 70) nextInt = map(nextInt, 65, 70, 10, 15);
if (nextInt >= 97 && nextInt <= 102) nextInt = map(nextInt, 97, 102, 10, 15);
nextInt = constrain(nextInt, 0, 15);
decValue = (decValue * 16) + nextInt;
}
return decValue;
}