isxdigit函数是干什么用的
时间: 2023-12-18 14:03:02 浏览: 21
isxdigit函数是C++中的标准库函数,也需要包含头文件<ctype.h>。它用于判断一个字符是否是十六进制数字,即0~9、a~f、A~F中的任意一个。
isxdigit函数的函数原型如下:
```c++
int isxdigit(int c);
```
其中,c为要判断的字符,函数返回值为非零表示c是十六进制数字,返回值为零表示c不是十六进制数字。
示例代码如下:
```c++
#include <iostream>
#include <ctype.h>
using namespace std;
int main()
{
char ch1 = '9';
char ch2 = 'a';
char ch3 = 'G';
if (isxdigit(ch1)) {
cout << ch1 << " is a hexadecimal digit." << endl;
} else {
cout << ch1 << " is not a hexadecimal digit." << endl;
}
if (isxdigit(ch2)) {
cout << ch2 << " is a hexadecimal digit." << endl;
} else {
cout << ch2 << " is not a hexadecimal digit." << endl;
}
if (isxdigit(ch3)) {
cout << ch3 << " is a hexadecimal digit." << endl;
} else {
cout << ch3 << " is not a hexadecimal digit." << endl;
}
return 0;
}
```
输出结果为:
```
9 is a hexadecimal digit.
a is a hexadecimal digit.
G is not a hexadecimal digit.
```
可以看到,isxdigit函数可以方便地判断一个字符是否是十六进制数字。