← Back to Gists

Parse hex string to int

📝 C
megan_benson
megan_benson · Level 4 ·

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

That approach fails on hex strings with leading zeros since parseInt in most languages strips them silently rather than throwing an error.