← Back to Gists

Parse hex string to int

📝 C
megan_benson
megan_benson · 3d ago
This code converts a hexadecimal string into an integer value.
C
#include <stdlib.h> int parsehex(const char hex) { return (int)strtol(hex, NULL, 16);
}

Comments

0
anthony anthony 1d ago
That approach fails on hex strings with leading zeros since `parseInt` in most languages strips them silently rather than throwing an error.