【问题标题】:Convert ASCII code to string in C在 C 中将 ASCII 码转换为字符串
【发布时间】:2017-08-01 00:49:53
【问题描述】:

我正在尝试基于单个 ASCII 代码创建一个字符数组。以下代码无法正确编译,即使 "num" 被强制转换为 char:

//Returns the ASCII counterpart of a number, such as 41 = A, 42 = B, 43 = C, etc.
char numToASCII(int num) {
    char[] string = {(char)num, "\0"};
    return string;
}

对于给我的任务,“字符串”是字符数组/字符串而不是单个字符非常重要。任何帮助将不胜感激。

【问题讨论】:

  • 您所做的几乎实际上是有效的...您需要使用字符而不是字符串作为空终止符:我认为char[] string = {(char)num, '\0'}; 将编译...
  • 但是不要返回那个,当你使用它时它会超出范围,要么malloc一些东西,要么传入内存
  • 这三行代码有很多问题。无论如何,您不能从 C 中的函数返回数组。您可以将数组传递给函数以保存字符串;你真的需要一个功能吗?改用复合文字怎么样?
  • 感谢您提供这些信息,@DavidBowling。我不知道 C 函数不能返回数组。考虑到这一点,我将不得不使用复合文字。
  • 我不知道你想在这里做什么,但你也可以使用snprintf()printf()(如果你想将它打印到标准输出)。不过,您仍然需要为字符串提供空间。像char s[2]; snprintf(&s[0], sizeof(s), "%c", num); 这样的东西。另外,请注意 41 不是“A”,0x41 是“A”。 :-)

标签: c arrays string


【解决方案1】:

数组必须初始化为常量表达式,如果你想返回一个数组,你的函数应该返回一个指针。 如果您只想返回一个字符,请改用以下代码:

char numToASCII(int num) {
    return (char)num;
}

如果要返回包含该字符的字符串,则应使用以下代码:

#include <stdlib.h>

char *numToASCII(int num) {
    /*
     * Use malloc to allocate an array in the heap, instead of using a
     * local array. The memory space of local array will be freed after
     * the invocation of numToASCII.
     */
    char *string = malloc(2);
    if (!string)
            return 0;
    string[0] = num;
    string[1] = 0;
    return string;
}

使用free()函数释放malloc()分配的空间。

【讨论】:

  • 现在你有内存泄漏。如果malloc() 失败了怎么办?
  • 如果 malloc(2) 失败,你只需要去睡觉......这不是你的日子......你的程序即将崩溃或被 OOM 怪物吃掉
  • @GradyPlayer 我们对这里的环境了解不多,但可能不是Linux。我见过没有 MMU 的系统,因此您甚至无法捕获对 NULL 指针的访问。我的意思不是繁重,只是要小心所做的假设。那样,编写无错误的代码就很难了。 :-)
【解决方案2】:

试试这个.. 你想找到 ASCII 码的字符,然后试试这个代码:

#include<stdio.h>
int main()
{
    int num;
    printf("\nEnter ASCII Code Number:\t");
    scanf("%d", &num);
    printf("\nASCII Value of %d: \t%c", num, num);
    printf("\n");
    return 0;
}

在此代码中,它将从用户那里获取 ASCII 码,并默认打印 ASCII 码的字符。

【讨论】:

    【解决方案3】:

    不确定这是否有帮助,但从文件中提取文本以 ascii 形式返回,我需要一个字符串并通过检查字符串长度来解决它,抱歉,因为我也是新手,所以需要额外的步骤。

    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *fp;
        char firstbuff[yourchoice];
        char secondbuff[yourchoice];
        char sentence[yourchoice];
        int stringlenght;
    
        fp = fopen("test.txt", "r");
    
        //Here add a means of counting the lines in the file as linecount
        for(int j = 0; j < linecount; j++)
        {
            fgets(firstbuff; 1000; fp);
     //get string length and use for loop to individually ascii copy as characters into array
            stringlength = strlen(firstbuff);
            for(int i = 0; i < stringlength; i++)
            {
               secondbuff[i] = (char)firstbuff[i];
            }
            //string concat
            strcat(sentence, secondbuff);
        }
        printf("%s\n", sentence);
        fclose(fp);
            
    }
    

    【讨论】:

    • strcat(sentence, secondbuff); 失败,因为 secondbuff 不是 字符串(缺少分配的空字符)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-09-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-03
    • 1970-01-01
    • 2011-01-21
    相关资源
    最近更新 更多