简体   繁体   English

如何找出,用户输入包含C中的非ASCII字符

[英]How to find out, that user input contains non ASCII characters in C

I'm reading user input with fgets() and I'm checking if there are some non allowed symbols. 我正在用fgets()读取用户输入,我正在检查是否有一些非允许的符号。

If user types "š" for example, I will notice it, because value of "š" is higher then 127. But when user types "ασδφ" or "жщдф", my code won't work, because these symbols are completely ignored and replaced by "?". 如果用户键入“š”,我会注意到它,因为“š”的值高于127.但是当用户键入“ασδφ”或“жщдф”时,我的代码将无法工作,因为这些符号完全被忽略并用“?”代替。

My code: 我的代码:

char input[100];
fgets(input, 100, stdin);
for (int i = 0; i < strlen(input) - 1; i++)
{
    /// Check, if input[i] is ASCII symbol
}

When user types "š", in variable input will be "š". 当用户输入“š”时,变量输入将为“š”。 But when user types "щ", int variable input will be "?" 但是当用户键入“ù”时,int变量输入将为“?” and question mark is valid ASCII character. 和问号是有效的ASCII字符。

How to fix it? 怎么解决?

EDIT: 编辑:

Operating system: Windows 10 操作系统:Windows 10

IDE: Visual Studio 2015 IDE:Visual Studio 2015

Code: 码:

for (size_t i = 0; i < strlen(input); i++)
{
    printf("%c %d\n", input[i], input[i]);
    if (input[i] < 0/* || input[i] > 127*/)
    {
        error = 4;
        break;
    }
}

If I pause a program, content of array input for user input "ασδφ" is 63, 63, 63, 63, 10. 如果我暂停一个程序,用户输入“ασδφ”的数组输入内容是63,63,63,63,10。

EDIT 2: 编辑2:

Now I'm totaly confused. 现在我完全糊涂了。 I tried compiling and running on Ubuntu, everything worked fine. 我尝试在Ubuntu上编译和运行,一切正常。 But on Windows it is still replacing non ASII symbols with questions marks. 但在Windows上,它仍然用问号替换非ASII符号。 Any idea how to get it work on Windows? 知道如何让它在Windows上运行吗?

I think you should use isascii(int ch) function defined in ctype.h header: 我认为你应该使用ctype.h头文件中定义的isascii(int ch)函数:

#include <ctype.h>

char input[100];
fgets(input, 100, stdin);
for (int i = 0; i < strlen(input) - 1; i++)
{
    if (isascii((int)input[i]) {
        /* If ASCII */
    } else {
       /*If non-ASCII */
    }
}

You can also use this : 你也可以用这个:

#include <stdio.h>

main(){
char input[100];
fgets(input, 100, stdin);
for (int i = 0; i < strlen(input) - 1; i++)
{
    if(input[i]<128 && input[i]>0)
        printf("\nASCII Value");
    else
        printf("\nNot an ASCII Value");
}
}

It is hard to tell from the details you've provided, but I do not think the problem is in your code. 从您提供的详细信息中很难说清楚,但我认为问题不在您的代码中。 When you mentioned that it works on Ubuntu, that hints that you are experiencing an encoding issue with your console. 当你提到它适用于Ubuntu时,暗示你遇到了控制台的编码问题。

If stdin is a file handle this shouldn't be an issue, but it sounds like you are trying to use stdin from the command line and copy/pasting input. 如果stdin是文件句柄,这应该不是问题,但听起来你正试图从命令行使用stdin并复制/粘贴输入。 Windows command prompt will convert unicode characters to '?' Windows命令提示符会将unicode字符转换为'?' if they are not supported by your console font. 如果您的控制台字体不支持它们。 See this question and the accepted answer for more information: 有关详细信息,请参阅此问题和接受的答案:

What encoding/code page is cmd.exe using? cmd.exe使用什么编码/代码页?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM