【问题标题】:How can I convert an int to a string (char*) in C如何在 C 中将 int 转换为字符串 (char*)
【发布时间】:2014-11-21 10:31:45
【问题描述】:

我一直在网上寻找这个,但我找不到任何东西。我目前有一个整数(int),我需要将其转换为char*(不是char[])。到目前为止,我无法让它以任何方式工作。谢谢

编辑: 我尝试做以下没有运气

int i = 8;
char *charArray = i + '0';

编辑 2: 因为我是 C 的新手,所以我在这里误解了一些东西。 我目前正在使用 Pebble SDK(智能手表),我正在尝试从 int 转换为 string 以将其传递给函数 text_layer_set_text() 当我使用格式时:char *example = "4" 我的代码运行良好。虽然,无论我尝试以哪种方式在层中传递我的整数都不会像预期的那样运行

【问题讨论】:

  • 能否请您发布一个示例(即使它是错误的,您是如何尝试的)?
  • 请显示您上次失败的尝试。
  • @Gopi char *example 肯定不是字符串文字。

标签: c arrays char integer character


【解决方案1】:

问题不清楚。

如果你想要一个char * 作为通用指针:

#include <stdio.h>

int main(void)
{
    int x = 5;
    char *p = (char *)&x;

    printf("%d\n", *(int *)p);
    return 0;
}

如果您想将char * 作为字符串:

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    int x = 5;
    char *p = malloc(32);

    sprintf(p, "%d", x);
    printf("%s\n", p);
    free(p);
    return 0;
}

编辑

我正在使用 pebble (smartwatch sdk) 并且 sprintf() 函数是 不支持

使用模除法得到每个数字,然后反转字符串:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(void)
{
    char *p = malloc(32);
    int i = 0, x = 5374;
    int temp, len;

    temp = abs(x); /* support for negatives */
    if (x < 0) p[i++] = '-'; /* support for negatives */
    do {
        p[i++] = '0' + temp % 10;
        temp /= 10;
    } while (temp);
    p[i] = '\0';
    if (x < 0) p++; /* support for negatives */
    len = strlen(p) - 1;
    for(i = 0; i < len / 2; i++) {
        temp = p[len];
        p[len] = p[i];
        p[i] = temp;
        len--;
    }
    if (x < 0) p--; /* support for negatives */
    printf("%s\n", p);
    free(p);
    return 0;
}

【讨论】:

  • 感谢您的回复。我正在使用 pebble (smartwatch sdk),不支持 sprintf() 函数。
  • 虽然我不完全理解代码,但您的示例为我的问题提供了可能的解决方案。谢谢
  • 你能给我一个个位数正整数的例子,以便我更容易理解代码是这样的吗?:char *p = malloc(32); int x = 5; p[i++] = '0' + x; p[i] = '\0';
  • @Rakim,是的!就是这样
  • 我不知道这个API,但是在提供的链接中有一个注释:The string is not copied, so its buffer most likely cannot be stack allocated
【解决方案2】:

检查以下代码:

int a = 1234;
char *str = malloc(20);
sprintf(str, "%d", a);
printf("%s\n",str);

【讨论】:

  • 我在问题中说我想要一个 char* 而不是 char[] 但感谢您的回复
【解决方案3】:
  1. 声明并定义一个char *数组,分配内存。
  2. 使用sprintf()。检查手册页here

自己编写代码,让我们知道输出。


编辑:

您始终可以将int 变量的地址转换为char *,但在使用char * 时要非常小心。

int i = 10;
char * p = NULL;
p = (char *)&i;

如果您想将*p 转换为表现为 整数,请不要忘记在使用前将p 转换回int *。否则,您会感到惊讶。

【讨论】:

  • 感谢您的回复。我用的是pebble(smartwatch sdk),不支持该功能。
  • 我没有收到任何错误或任何内容,但我的文本层中没有显示任何内容。我确定其余代码没有任何问题,因为将char *example = "8" 作为参数传递给函数效果很好
  • @Rakim 我想,我(以及其他所有人,就此而言)误解了你的问题。 char *example = "8" 是一个完全不同的故事。请详细说明您的问题。
  • 您能否进一步解释一下,因为我是 C 新手,但我不明白其中的区别?谢谢
  • @Rakim 肯定会的。但是,在此之前,您需要告诉我们您究竟想要什么。
【解决方案4】:

您的第一次尝试会奏效——如果您要使用char 而不是char*

int i = 8;
char c  = (char) (i + '0');    // c = "8"

该方法在技术上不能保证有效 - 因为标准不会假定您使用的是 ASCII、UNICODE 或 EBCDIC - 但使用除这三个之外的某些字符系统几乎是闻所未闻的,因此您很安全.它也只适用于 [0-9] 范围内的整数。

【讨论】:

    【解决方案5】:

    一个简单的方法是:

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    ....
    char buffer[32] = {'\0'};
    
    int  a = 23;
    
    itoa(a,buffer,10);   // here 10 means decimal
    ...
    

    【讨论】:

      【解决方案6】:

      你给出的例子,char *example = "8"// 这应该是 char *example = '8';您正在创建指向 char 而不是字符串的指针,而是一个字符数组。

      您想要一个将 int 表示为 ascii 的字符数组? (或unicode?)

      char buffer[32] = {'\0'}; //or char *buffer = malloc(32 * sizeof(char));  
      int a = 10;
      itoa(a, buffer,10);//convert an integer that can be represented by up to 32 chars
      

      查看了 pebble api(30 秒),找到了 APIs sprintf 版本,值得一看吗? int snprintf(char * str, size_t n, const * fmt, ... )

      【讨论】:

      • 如果在 iota 中使用 *buffer 可能需要 &buffer,试试吧
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-03-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多