cybersecurity · intermediate · ~15 min
Validate fixed-width hex input.
Parse exactly two hex digits into a byte value, rejecting anything that is not precisely two valid hex characters — the fixed-width discipline binary protocols need.
Implement int parse_hex_byte(const char *s, int *out) that parses the first two characters of s as hex digits into a value 0..255. Return 1 and write the value to *out on success; return 0 if s is not exactly two hex digits.
s: a NUL-terminated string the grader provides.out: where the parsed byte value is written on success.Returns 1 on success (with *out in 0..255), 0 otherwise.
parse_hex_byte("ff", &out) -> 1, out = 255
parse_hex_byte("1a", &out) -> 1, out = 26
parse_hex_byte("f", &out) -> 0 (too short)
parse_hex_byte("zz", &out) -> 0 (not hex)
A NUL-terminated string s and an out pointer.
1 with *out (0..255) if s is exactly two hex digits; otherwise 0.
Require exactly two hex digits; accept upper- and lower-case.
int parse_hex_byte(const char *s, int *out) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.