
The ctype isxdigit() function returns TRUE for any digit you find in a hexadecimal number. I suppose this function has a role to play, most likely in detecting hexadecimal characters in a string. Regardless, this post wraps up my exploration of the ctype functions.
Hexadecimal values include numbers zero through 9 plus the letters A through F, both upper- and lowercase, to represent values 0 through 15. This counting base (16 or hexadecimal) is common in computerdom as it provides a quick way to reference bits, which are typically arranged in groups of four:
| Bits | Hex | Decimal |
| 0000 | 0 | 0 |
| 0001 | 1 | 1 |
| 0010 | 2 | 2 |
| 0011 | 3 | 3 |
| 0100 | 4 | 4 |
| 0101 | 5 | 5 |
| 0110 | 6 | 6 |
| 0111 | 7 | 7 |
| 1000 | 8 | 8 |
| 1001 | 9 | 9 |
| 1010 | A | 10 |
| 1011 | B | 11 |
| 1100 | C | 12 |
| 1101 | D | 13 |
| 1110 | E | 14 |
| 1111 | F | 15 |
Truly, you can’t consider yourself a nerd until you know how to count in hex, or can quickly translate hex values such as 0x20 and 0x41 into their decimal equivalents and vice-versa.
I went to a restaurant where the waiter wore a shirt with a series of hex values. I don’t need to tell you that I was only customer ever to read the text those codes represented. (I forget what it said, specifically.) #nerd
The isxdigit() function returns TRUE if the character examined is a number, 0 through 9, uppercase letter, A through Z, or lowercase letter, a through z.
Here is sample code that plows through the printable ASCII code values and plucks out hex digits:
2026_06_27-Lesson-a.c
#include <stdio.h>
#include <ctype.h>
int main()
{
int ch;
puts("X Digits:");
/* ASCII character range */
for( ch=' '; ch<='~'; ch++ )
if( isxdigit(ch) )
putchar(ch);
putchar('\n');
return 0;
}
The for loop runs the gamut of ASCII codes from space through tilde. I could have used the isprint() function here, but didn’t want to stack up too many custom ctype functions in my version of the code (below).
In the if test, the isxdigit() function returns TRUE for a hexadecimal digit, which is output as shown in this sample run:
X Digits: 0123456789ABCDEFabcdef
To emulate the isxdigit() function, I use a series of if tests to weed out the three ranges of ASCII characters found in a hexadecimal value: 0 to 9, A to Z, and a to z:
2026_06_27-Lesson-b.c
#include <stdio.h>
#include <stdbool.h>
int isxdigit(int c)
{
if( c>='0' && c<='9' )
return true;
if( c>='A' && c<='F' )
return true;
if( c>='a' && c<='f' )
return true;
return false;
}
int main()
{
int ch;
puts("X Digits:");
/* ASCII character range */
for( ch=' '; ch<='~'; ch++ )
if( isxdigit(ch) )
putchar(ch);
putchar('\n');
return 0;
}
The program’s output is the same.
To conclude this series, I would comment that the ctype functions don’t really carry a lot of weight in the Unicode era. It would be nice to see extended ctype functions available that truly understand different locales. Then again, with a knowledge of a programming language like C, you can always code your own functions. Heck, draw up a library! Anything is possible.