std::isxdigit(3)
π NAME π
std::isxdigit β literally std::isxdigit, that's the whole name bestie, we're not doing anything fancy here π
This page lives in the C++ Standard Libary manual, section 3 β and yes that's their typo, not mine, I would never π but I'm keeping it because facts are facts.
β¨ SYNOPSIS β¨
Okay so first of all it's defined in header <cctype>, and the signature is exactly this:
int isxdigit( int ch );
What it does: checks if the given character is a hexadecimal numeric character β and the hex crew is exactly:
0123456789abcdefABCDEF
That's it. That's the whole squad π
β οΈ Big caveat, pay attention bestie: the behavior is undefined if the value ofchis not representable asunsigned charand is not equal toEOF. Undefined = β¨anything can happenβ¨ = not the vibe we want.
πΈ PARAMETERS πΈ
chβ the character to classify π«Ά (what's its deal, what's its classification, we're about to find out)
π RETURN VALUE π
Non-zero value if the character is a hexadecimal numeric character, zero otherwise. That's it. Binarily yes or no, icon behavior.
π NOTES π
Okay this part is lowkey the important part so listen up π
std::isdigit and std::isxdigit are the only standard narrow character classification functions that are not affected by the currently installed C locale β although some implementations (e.g. Microsoft in 1252 codepage) may classify additional single-byte characters as digits. So likeβ¦ mostly locale-proof, but not 100% universally, iykyk.
And like all other functions from <cctype>, the behavior of std::isxdigit is undefined if the argument's value is neither representable as unsigned char nor equal to EOF ππ
To use these functions safely with plain chars (or signed chars), convert the argument to unsigned char first, like this:
bool my_isxdigit(char ch)
{
return std::isxdigit(static_cast<unsigned char>(ch));
}
Similarly, they should not be directly used with standard algorithms when the iterator's value type is char or signed char. Instead, convert the value to unsigned char first:
int count_xdigits(const std::string& s)
{
return std::count_if(s.begin(), s.end(),
// static_cast<int(*)(int)>(std::isxdigit) // wrong
// [](int c){ return std::isxdigit(c); } // wrong
// [](char c){ return std::isxdigit(c); } // wrong
[](unsigned char c){ return std::isxdigit(c); } // correct
);
}
Note the three commented-out attempts marked // wrong β those are the traps, do not step in them π© The [](unsigned char c) one is marked // correct, be like that one β
π¦ EXAMPLE π¦
Run this code, it's cute:
#include <cctype>
#include <climits>
#include <iostream>
int main()
{
for (int c = 0; UCHAR_MAX >= c; ++c)
if (isxdigit(c))
std::cout << static_cast<char>(c);
std::cout << '\n';
}
π OUTPUT π
0123456789ABCDEFabcdef
It printed the entire hex alphabet twice (lower + upper) and honestly? Slayed π₯
π SEE ALSO π
isxdigit(std::locale)β checks if a character is classified as a hexadecimal digit by a locale β (function template)iswxdigitβ checks if a wide character is a hexadecimal character β (function)- C documentation for
isxdigit
And then there's the giant ASCII table of destiny π every ASCII value, its characters, and which of the classification functions say yes to it β narrow ones (iscntrl isprint isspace isblank isgraph ispunct isalnum isalpha isupper islower isdigit isxdigit) plus the wide ones (iswcntrl iswprint iswspace iswblank iswgraph iswpunct iswalnum iswalpha iswupper iswlower iswdigit iswxdigit). Dropping it raw so nothing gets lost π
ASCII values characters iscntrl isprint isspace isblank isgraph ispunct isalnum isalpha isupper islower isdigit isxdigit decimal hexadecimal octal iswcntrl iswprint iswspace iswblank iswgraph iswpunct iswalnum iswalpha iswupper iswlower iswdigit iswxdigit
0β8 \x0β\x8 \0β\10 control codes β 0 0 0 0 0 0 0 0 0 0 0
(NUL, etc.)
9 \x9 \11 tab (\t) β 0 0 β 0 β 0 0 0 0 0 0 0 0 0
whitespaces
10β13 \xAβ\xD \12β\15 (\n, \v, \f, β 0 0 β 0 0 0 0 0 0 0 0 0 0
\r)
14β31 \xEβ\x1F \16β\37 control codes β 0 0 0 0 0 0 0 0 0 0 0 0
32 \x20 \40 space 0 β 0 β 0 β 0 0 0 0 0 0 0 0 0
33β47 \x21β\x2F \41β\57 !"#$%&'()*+,-./ 0 β 0 0 0 β 0 β 0 0 0 0 0 0 0
48β57 \x30β\x39 \60β\71 0123456789 0 β 0 0 0 β 0 0 β 0 0 0 0 β 0 β 0
58β64 \x3Aβ\x40 \72β\100 :;<=>?@ 0 β 0 0 0 β 0 β 0 0 0 0 0 0 0
65β70 \x41β\x46 \101β\106 ABCDEF 0 β 0 0 0 β 0 0 β 0 β 0 β 0 0 0 β 0
71β90 \x47β\x5A \107β\132 GHIJKLMNOP 0 β 0 0 0 β 0 0 β 0 β 0 β 0 0 0 0
QRSTUVWXYZ
91β96 \x5Bβ\x60 \133β\140 [\]^_` 0 β 0 0 0 β 0 β 0 0 0 0 0 0 0
97β102 \x61β\x66 \141β\146 abcdef 0 β 0 0 0 β 0 0 β 0 β 0 0 β 0 0 β 0
103β122 \x67β\x7A \147β\172 ghijklmnop 0 β 0 0 0 β 0 0 β 0 β 0 0 β 0 0 0
qrstuvwxyz
123β126 \x7Bβ\x7E \172β\176 {|}~ 0 β 0 0 0 β 0 β 0 0 0 0 0 0 0
127 \x7F \177 backspace β 0 0 0 0 0 0 0 0 0 0 0 0
character (DEL)
And that's the whole page! Source and version teaser: this came from http://cppreference.com, dated 2024.06.10, filed under std::isxdigit(3) β go convert your chars to unsigned char and stay safe out there bestie π«Άπβ¨
π¦ where this page comes from
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms