C++ isxdigit()

The isxdigit() function in C++ checks if the given character is a hexadecimal character or not.

isxdigit() Prototype

int isxdigit(int ch);

The isxdigit() function checks if ch is a hexadecimal numeric character as classified by the current C locale. The available hexadecimal numeric characters are:

  • Digits (0 to 9)
  • Lowercase alphabets from a to f
  • Uppercase alphabets from A to F

The behaviour of isxdigit() is undefined if the value of ch is not representable as unsigned char or is not equal to EOF.

It is defined in <cctype> header file.

isxdigit() Parameters

ch: The character to check.

isxdigit() Return value

The isxdigit() function returns non zero value if ch is a hexadecimal character, otherwise returns zero.

Example: How isxdigit() function works

#include <cctype>
#include <iostream>
#include <cstring>

using namespace std;

int main()
{
    bool flag = 0;
    char str[] = "50Af";

    for (int i=0; i<strlen(str); i++)
    {
        if (!isxdigit(str[i]))
        {
            flag = 1;
            break;
        }
    }

    if (flag)
        cout << str << " is not a valid hexadecimal number";
    else
        cout << str << " is a valid hexadecimal number";
    
    return 0;
}

When you run the program, the output will be:

50Af is a valid hexadecimal number