iswxdigit() function in C/C++

The iswxdigit() is a built-in function in C/C++ which checks if the given wide character is a hexadecimal digit character or not. It is defined within the cwctype header file of C++. The available hexadecimal numeric characters are:

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

Syntax:

int iswxdigit(ch)

Parameter: The function accepts a single mandatory parameter ch which specifies the wide character which we have to check that if it is a hexadecimal or not.



Return Value: The function returns two values as shown below.

  • If the ch is a hexa decimal, then a non-zero value is returned.
  • If it is not a hexa decimal then 0 is returned.

Below programs illustrates the above function.

Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// iswxdigit() function
#include <cwchar>
#include <cwctype>
#include <iostream>
using namespace std;
  
// function to check if
// the wide character is hexadecimal or not
void ishexadecimal(wchar_t* str)
{
    bool flag = false;
    for (int i = 0; i < wcslen(str); i++) {
        if (!iswxdigit(str[i])) {
            flag = true;
            break;
        }
    }
  
    if (flag)
        wcout << str << L" is not a valid"
              << " hexadecimal number" << endl;
    else
        wcout << str << L" is a valid"
              << " hexadecimal number" << endl;
}
  
// Driver Code
int main()
{
    wchar_t str[] = L"a3lz";
    ishexadecimal(str);
  
    wchar_t str1[] = L"10dbe";
    ishexadecimal(str1);
  
    return 0;
}

chevron_right


Output:

a3lz is not a valid hexadecimal number
10dbe is a valid hexadecimal number

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ program to illustrate
// iswxdigit() function
#include <cwchar>
#include <cwctype>
#include <iostream>
using namespace std;
  
// function to check if
// the wide character is hexadecimal or not
void ishexadecimal(wchar_t* str)
{
    bool flag = false;
    for (int i = 0; i < wcslen(str); i++) {
        if (!iswxdigit(str[i])) {
            flag = true;
            break;
        }
    }
  
    if (flag)
        wcout << str << L" is not a valid"
              << " hexadecimal number" << endl;
    else
        wcout << str << L" is a valid"
              << " hexadecimal number" << endl;
}
  
// Driver Code
int main()
{
    wchar_t str[] = L"1441a";
    ishexadecimal(str);
  
    wchar_t str1[] = L"xyz2";
    ishexadecimal(str1);
  
    return 0;
}

chevron_right


Output:

1441a is a valid hexadecimal number
xyz2 is not a valid hexadecimal number

Similar Functions : isalpha() and isdigit() functions in C/C++ with example



My Personal Notes arrow_drop_up

Budding Web DeveloperKeen learnerAverage CoderDancer&Social Activist

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.