如何将字符串字符的ASCII值解释为C中的字符?

发布于 2025-02-05 07:53:05 字数 1658 浏览 1 评论 0原文

我有一系列代表字母字符[a,z],[a,z]和“” ASCII值的数字序列。我可以在char中获得一个值之一的ASCII值(即char = 76),但是当我尝试进行界限检查并将其附加到结果时,我似乎无法获得“ L”而不是76和76和不能将CHAR值与边界的ASCII值进行比较(即A = 65)。我该如何在C中这样做?

//input would be something like char* source = "76111114..."
// result should be "Lor"
for(int i = 0; i < strLen; i+=2)
{
    char actualChar = ' ';
    
    //get two numbers as a character
    strncpy(&actualChar, source + i, 2);
    
    //this prints out 76
    printf("%s\n", &actualChar);

    //this prints out 55
    printf("%d\n", actualChar);
    
    
    //check if the character is a space
    if(actualChar == 32)
    {
        //append
         strncat(result, &actualChar, 2);
         printf("Added space : %s\n", result);
         continue;
    }
    
    //[A, Z]
    else if(actualChar >= 65 && actualChar <= 90)
    {
        //append
        strncat(result, &actualChar, 2);
        printf("Added 2 digit char: %s, %s\n", &actualChar ,result);
        continue;
    }
    
    else
    {
        //add one number to the char and check again
        strncpy(&actualChar, source + i, 3);
        
        //[a, z]
        if(actualChar >= 97 && actualChar <= 122)
        {
            ++i;
            
            //append
            strncat(result, &actualChar, 3);
            printf("Added 3 digit char: %s, %s\n", &actualChar ,result);
            
            continue;
        }
        
        //not a valid char
        else
        {
            printf("Not valid char: %s", &actualChar);
            return 0;
        }
    }
}

我希望结果是“ lor”,但我得到的是“ 7611114”。

I have a sequence of numbers representing ASCII values of alphabet characters [A,Z], [a,z] and " ". I can get in a char the ascii value of one of the values (i.e. char = 76), but when I try to make the boundaries checking and the appending to the result I can't seem to get "L" instead of 76 and cant compare the char value to the ASCII values of the boundaries (i.e A = 65). How do I do this in C?

//input would be something like char* source = "76111114..."
// result should be "Lor"
for(int i = 0; i < strLen; i+=2)
{
    char actualChar = ' ';
    
    //get two numbers as a character
    strncpy(&actualChar, source + i, 2);
    
    //this prints out 76
    printf("%s\n", &actualChar);

    //this prints out 55
    printf("%d\n", actualChar);
    
    
    //check if the character is a space
    if(actualChar == 32)
    {
        //append
         strncat(result, &actualChar, 2);
         printf("Added space : %s\n", result);
         continue;
    }
    
    //[A, Z]
    else if(actualChar >= 65 && actualChar <= 90)
    {
        //append
        strncat(result, &actualChar, 2);
        printf("Added 2 digit char: %s, %s\n", &actualChar ,result);
        continue;
    }
    
    else
    {
        //add one number to the char and check again
        strncpy(&actualChar, source + i, 3);
        
        //[a, z]
        if(actualChar >= 97 && actualChar <= 122)
        {
            ++i;
            
            //append
            strncat(result, &actualChar, 3);
            printf("Added 3 digit char: %s, %s\n", &actualChar ,result);
            
            continue;
        }
        
        //not a valid char
        else
        {
            printf("Not valid char: %s", &actualChar);
            return 0;
        }
    }
}

I would like result to be "Lor" but instead I am getting "76111114".

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

烟燃烟灭 2025-02-12 07:53:06

如评论中提到的,sscanf(源,“%2D”,&amp; muthChar)和sscanf(来源,“%3D”,&amp; natualChar)是获得它的最简单方法,尽管我必须将其施放为int*才能使SSCANF工作:

sscanf(source+ i,“%2D”,(int*),(&amp; amp; muthatch));>

As mentioned in the comments, sscanf(source, "%2d", &actualChar) and sscanf(source, "%3d", &actualChar)is the easiest way to get it, although I had to cast actualChar it to int* for the sscanf to work:

sscanf(source+ i, "%2d", (int*)(&actualChar));

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文