How do I check whether character constants conform to ASCII?

半腔热情 提交于 2019-12-23 19:33:55

问题


A comment on an earlier version of this answer of mine alerted me to the fact that I can't assume that 'A', 'B', 'C' etc. have successive numeric values. I had sort of assumed the C or C++ language standards guarantee that this is the case.

So, how should I determine whether consecutive letter characters' values are themselves consecutive? Or rather, how can I determine whether the character constants I can express within single quotes have their ASCII codes for a numeric value?

I'm asking how to do this both in C and in C++. Obviously the C way would work in C++ also, but if there's a C++ish facility for doing this I'm interested in that as well. Also, I'm asking about the newest relevant standards (C11, C++17).


回答1:


You can use the preprocessor to check if a particular character maps to the charset:

#include <iostream>
using namespace std;

int main() {
    #if ('A' == 65 && 'Z' - 'A' == 25)
    std::cout << "ASCII" << std::endl;
    #else
    std::cout << "Other charset" << std::endl;
    #endif
    return 0;
}

The drawback is, you need to know the mapped values in advance.

The numeric chars '0' - '9' are guaranteed to appear in consecutive order BTW.




回答2:


... (2) I expect to be able to obtain the distance in number-of-letters between two letters ...

This comment specifying your goal makes much more sense than your actual question! Why didn't you ask about that? You can use strchr on an array of characters, and strchr doesn't care what the native character set is, meaning your code won't care what the native character set is... For example:

char alphabet[] = "AaBbCcDdEeFfGgHhIiJjKkLlMmNnOoPpQqRrSsTtUuVvWwXxYyZz";
ptrdiff_t fubar = strchr(alphabet, 'y') - strchr(alphabet, 'X');
printf("'X' and 'y' have a distance of %tu and a case difference of %tu\n", fubar / 2, fubar % 2);

... how should I determine whether consecutive letter characters' values are themselves consecutive?

Consecutive letter characters' values are consecutive, by definition, because they're consecutive letter characters. I know this isn't what you meant, but your actual question illustrates a lack of planning and thought, and... a stupid question warrants a stupid answer.

You're much better off programming in such a way that you don't care what values they have. Nonetheless, create an array containing the characters you care about, loop through the elements and test for inconsistencies. For example:

int is_consecutive(char const *alphabet) {
    for (size_t x = 0; alphabet[x] && alphabet[x] + 1 == alphabet[x + 1]; x++);
    return !alphabet[x];
}

... how can I determine whether the character constants I can express within single quotes have their ASCII codes for a numeric value?

Again with the lack of sense, and again with the caring about values... Alternatively, build two translation tables, native_to_ascii and ascii_to_native, and work it out from there. I won't help you with this, as it's a silly exercise involving the use of magic numbers that most likely aren't necessary for your actual goal.



来源:https://stackoverflow.com/questions/42056535/how-do-i-check-whether-character-constants-conform-to-ascii

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!