简体   繁体   English

将Int转换为char *指针

[英]Converting Int to a char* pointer

I'm trying to convert an integer to a char* pointer so I can properly send the argument into another function. 我正在尝试将整数转换为char *指针,以便可以将参数正确发送到另一个函数中。 Is there anyway to do this without atoi? 反正没有atoi可以做到这一点吗?

int number=2123, number2= 1233; 
char* arg[];

arg[0] = number;
arg[1] = number2;

**Sorry for not making things clear so basically I want the arg[0] to equal the string rep of the number, so that i can send it into a function like this: converted(char* arg); **对不起,我基本上不希望让事情变得清楚,我希望arg [0]等于数字的字符串rep,以便我可以将其发送到这样的函数中:converted(char * arg);

i would change the parameter data type but it has to be that pointer to send in. 我会更改参数数据类型,但必须是要发送的指针。

to convert an integer to a char* pointer so I can properly send the argument into another function. 将整数转换为char*指针,以便我可以正确地将参数发送到另一个函数中。

Create a string representation of the number by calling a helper function with a char array via a compound literal to convert the number. 通过使用带有复合文字char数组调用char函数来转换数字,从而创建数字的字符串表示形式。 The string will be valid for the function and to the end of the block of code. 该字符串将对该函数有效,并在代码块的末尾有效。 Adequate sizing of the string is important - leave that for a separate exercise. 字符串的适当大小很重要-将其留给另一个练习。 Code here uses 41, enough for 128 bit int . 这里的代码使用41,足以用于128位int No need for a memory management via malloc()/free() for such a small buffer. 对于这么小的缓冲区,不需要通过malloc()/free()进行内存管理。

char *my_itoa(char *dest, int i) {
  sprintf(dest, "%d", i);
  return dest;
}

#define ITOA(n) my_itoa((char [41]) { 0 }, (n) )

int main(void) {
  int number=2123, number2= 1233;
  printf("<%s> <%s>\n",  ITOA(number),  ITOA(number2));
  printf("<%s> <%s> <%s>\n",  ITOA(INT_MIN),  ITOA(0),  ITOA(INT_MAX));
}

Output 输出量

<2123> <1233>
<-2147483648> <0> <2147483647>

If you assume that a pointer is always at least the same size as an integer (are there any exceptions to this?) you can safely convert integers to pointers and back with these macros: 如果您假设指针的大小始终至少与整数相同(是否有例外情况?),您可以安全地将整数转换为指针,并使用以下宏将其返回:

#define INT2POINTER(a) ((char*)(intptr_t)(a))
#define POINTER2INT(a) ((int)(intptr_t)(a))

Or if that other function isn't your function, but a function that wants the integers as string you could use asprintf() like this: 或者,如果该其他函数不是您的函数,而是想要将整数作为字符串的函数,则可以这样使用asprintf():

asprintf(&arg[0],"%d",number);
asprintf(&arg[1],"%d",number2);

But asprintf is not posix standard so it might not be available on all systems. 但是asprintf不是posix标准,因此可能并非在所有系统上都可用。 You could use sprintf() (or snprintf() so be on the safe side) instead, but then you need to calculate the string length first. 您可以改用sprintf()(或snprintf()以便安全起见),但是随后您需要首先计算字符串长度。

Here is the function you"re looking for: 这是您要寻找的功能:

char    *int_to_char(int nb)
{
  int   div;
  char  *str;
  int   i;
  int   size;
  int   sign;

  i = -1;
  sign = 1;
  size = get_int_lenght(nb);
  if (!(str = malloc(sizeof(char) * (size + 1))))
    return (NULL);
  if (nb < 0)
    {
      sign *= -1;
      nb *= -1;
    }
  if (nb == 0)
    {
      str[++i] = '0';
      str[i + 1] = 0;
      return (str);
    }
  while (nb > 0)
    {
      str[++i] = (nb % 10) + '0';
      nb /= 10;
    }
  if (sign < 0)
    str[++i] = '-';
  str[i + 1] = 0;
  return (my_revstr(str));
}

With revstr function: 具有revstr功能:

char    *my_revstr(char *str)
{
  int   i;
  int   j;
  char  tmp;

  i = -1;
  j = strlen(str);
  while (++i < j)
    {
      tmp = str[i];
      str[i] = str[--j];
      str[j] = tmp;
    }
  return (str);
}

And get_int_lenght function: 和get_int_lenght函数:

int     get_int_lenght(int nb)
{
  int   size;

  size = 0;
  if (nb <= 0)
    {
      size++;
      nb *= -1;
    }
  while (nb > 0)
    {
      size++;
      nb /= 10;
    }
  return (size);
}

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

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