简体   繁体   English

在字符串中搜索垃圾字符

[英]Searching for Junk Characters in a String

Friends朋友们

I want to integrate the following code into the main application code.我想将以下代码集成到主应用程序代码中。 The junk characters that come populated with the o/p string dumps the application The following code snipette doesnt work..使用 o/p 字符串填充的垃圾字符会转储应用程序以下代码片段不起作用..

void stringCheck(char*);

int main()
{
    char some_str[] = "Common Application FE LBS Serverr is down";
    stringCheck(some_str);
}


void stringCheck(char * newString)
{
    for(int i=0;i<strlen(newString);i++)
    {
        if ((int)newString[i] >128)
        {

TRACE(" JUNK Characters in  Application Error message FROM DCE IS = "<<(char)newString[i]<<"++++++"<<(int)newString[i]);

        }
    }
}

Can someone please show me the better approaches to find junk characters in a string..有人可以告诉我在字符串中查找垃圾字符的更好方法吗?

Many Thanks非常感谢

Your char probably is represented signed.您的char可能已签名。 Cast it to unsigned char instead to avoid that it becomes a negative integer when casting to int :将其转换为unsigned char以避免在转换为int时它变为负 integer :

if ((unsigned char)newString[i] >128)

Depending on your needs, isprint might do a better job, checking for a printable character, including space:根据您的需要, isprint可能会做得更好,检查可打印字符,包括空格:

if (!isprint((unsigned char)newString[i])) 
    ...

Note that you have to cast to unsigned char : input for isprint requires values between 0 and UCHAR_MAX as character values.请注意,您必须强制转换为unsigned charisprint的输入需要0UCHAR_MAX之间的值作为字符值。

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

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