C isxdigit()

isxdigit() 函数定义在 ctype.h 头文件中。它用于检查指定字符是否为十六进制数字(0-9, a-f, A-F)字符。


int isxdigit( int argument ); #where argument will be a character
 

isxdigit() 参数

isxdigit() 函数接受一个整数形式的参数,返回值也应为整数。当传入字符时,它会被转换为对应的 ASCII 值。

参数 描述 必需/可选
参数 要检查的字符 必需

isxdigit() 返回值

如果给定字符是十六进制字符,isxdigit() 返回非零整数,否则返回零。当传入十六进制字符时,我们会得到不同的非零整数。

输入 返回值
如果参数不是十六进制字符
非零数字 如果参数是十六进制字符

isxdigit() 示例

示例 1: 如何在 C 语言中检查十六进制字符?


#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;
    ch='5';
    // hexadecimal character
    printf("If a hexadecimal character is passed: %d", isxdigit(ch));

    ch='+';
    // non-hexadecimal character
    printf("\nIf a non-hexadecimal character is passed: %d", isxdigit(ch));

    return 0;
}
 

输出


If a hexadecimal character is passed: 128
If a non-hexadecimal character is passed: 0

示例 2: 如何检查字符是否为 十六进制


#include <stdio.h>
#include <ctype.h>

int main()
{
    char ch;

    printf("Enter any character: ");
    scanf("%c",&ch);

    if (isxdigit(ch) == 0)
         printf("The given %c is not a hexadecimal.",ch);
    else
         printf("The given %c is a hexadecimal.",ch);
    return 0;
}
 

输出


Enter any character: 4
The given 4 is a hexadecimal.