Parse hex string to int
📝 CThis code converts a hexadecimal string into an integer value.
C
#include <stdlib.h> int parsehex(const char hex) { return (int)strtol(hex, NULL, 16);
}
This code converts a hexadecimal string into an integer value.
#include <stdlib.h> int parsehex(const char hex) { return (int)strtol(hex, NULL, 16);
}
Comments
That approach fails on hex strings with leading zeros since
parseIntin most languages strips them silently rather than throwing an error.