Monday, July 27, 2026
HomeC ProgrammingCtype Operate: isxdigit() | C For Dummies Weblog

Ctype Operate: isxdigit() | C For Dummies Weblog



The ctype isxdigit() operate returns TRUE for any digit you discover in a hexadecimal quantity. I suppose this operate has a task to play, most probably in detecting hexadecimal characters in a string. Regardless, this submit wraps up my exploration of the ctype features.

Hexadecimal values embody numbers zero by 9 plus the letters A by F, each upper- and lowercase, to symbolize values 0 by 15. This counting base (16 or hexadecimal) is frequent in computerdom because it gives a fast solution to reference bits, that are usually organized in teams of 4:

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

Really, you’ll be able to’t contemplate your self a nerd till you understand how to rely in hex, or can shortly translate hex values akin to 0x20 and 0x41 into their decimal equivalents and vice-versa.

I went to a restaurant the place the waiter wore a shirt with a sequence of hex values. I don’t have to let you know that I used to be solely buyer ever to learn the textual content these codes represented. (I neglect what it mentioned, particularly.) #nerd

The isxdigit() operate returns TRUE if the character examined is a quantity, 0 by 9, uppercase letter, A by Z, or lowercase letter, a by z.

Right here is pattern code that plows by the printable ASCII code values and plucks out hex digits:

2026_06_27-Lesson-a.c


#embody <stdio.h>
#embody <ctype.h>

int foremost()
{
    int ch;

    places("X Digits:");
    
    for( ch=" "; ch<='~'; ch++ )
        if( isxdigit(ch) )
            putchar(ch);
    putchar('n');

    return 0;
}

The for loop runs the gamut of ASCII codes from house by tilde. I may have used the isprint() operate right here, however didn’t wish to stack up too many customized ctype features in my model of the code (under).

Within the if check, the isxdigit() operate returns TRUE for a hexadecimal digit, which is output as proven on this pattern run:

X Digits:
0123456789ABCDEFabcdef

To emulate the isxdigit() operate, I take advantage of a sequence of if checks to weed out the three ranges of ASCII characters present in a hexadecimal worth: 0 to 9, A to Z, and a to z:

2026_06_27-Lesson-b.c


#embody <stdio.h>
#embody <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 foremost()
{
    int ch;

    places("X Digits:");
    
    for( ch=" "; ch<='~'; ch++ )
        if( isxdigit(ch) )
            putchar(ch);
    putchar('n');

    return 0;
}

This system’s output is similar.

To conclude this sequence, I might remark that the ctype features don’t actually carry a variety of weight within the Unicode period. It could be good to see prolonged ctype features accessible that actually perceive completely different locales. Then once more, with a information of a programming language like C, you’ll be able to all the time code your individual features. Heck, draw up a library! Something is feasible.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments