C语言 如何在linux(gcc)中将int转换为char/string,反之亦然?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6746244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 09:10:04  来源:igfitidea点击:

How to convert int to char/string and vice versa in linux(gcc)?

csocketsgccprintf

提问by SP Sandhu

I want to know the method of converting an integer into char/string and vice-versa also.

我想知道将整数转换为字符/字符串的方法,反之亦然。

I have already used sprintf(&charvar,"%d",&intvar) but it produces wrong output, possibly garbage.

我已经使用了 sprintf(&charvar,"%d",&intvar) 但它产生了错误的输出,可能是垃圾。

i have also heard atoi() in gcc has bugs.Reference:GCC atoi bug

我也听说 gcc 中的 atoi() 有bug。参考:GCC atoi bug

What is the other method to convert string/char back to int ?

将 string/char 转换回 int 的另一种方法是什么?

Actually i want to send an integer from one machine to another using SOCK_STREAM .

实际上我想使用 SOCK_STREAM 将一个整数从一台机器发送到另一台机器。

//EDIT : I forgot to tell that sprintf() does conversion and returns positive value.

//编辑:我忘了告诉 sprintf() 进行转换并返回正值。

采纳答案by Anders Abel

If you want to send an integer to another machine you can send it as binary data, just by sending the intvardirectly to the stream, you don't have to convert it to a char first. That will only introduce problems with knowing the length of the data as different values generate different lengths of strings.

如果您想将整数发送到另一台机器,您可以将其作为二进制数据发送,只需将intvar直接发送到流,您不必先将其转换为字符。这只会在了解数据长度方面引入问题,因为不同的值会生成不同长度的字符串。

回答by NPE

Remove the ampersand before intvar:

删除之前的&符号intvar

sprintf(&charvar,"%d",intvar)

Two notes:

两个注意事项:

  • Here, I assume that &charvaris of correct type, which it probably isn't.
  • Even though it might not make much difference here, it's a good to get into the habit of using snprintfin preference to sprintf.
  • 在这里,我假设它&charvar是正确的类型,但它可能不是。
  • 尽管在这里可能没有太大区别,但养成snprintf优先使用sprintf.

Here's some example code:

下面是一些示例代码:

int intvar = ...;
char str[16];
snprintf(str, sizeof(str), "%d", intvar);

回答by Stan

Please read the manual of 'sprintf' and 'sscanf', and maybe their safer versions are proper for you.

请阅读 'sprintf' 和 'sscanf' 的手册,也许它们更安全的版本适合您。

回答by Vlad

You cannot sprintfto a variable. You need a buffer for it, because of possible several digits and the trailing zero. Moreover, the argument should be the int variable, not its address.

你不能sprintf对一个变量。您需要一个缓冲区,因为可能有几个数字和尾随零。此外,参数应该是 int 变量,而不是它的地址。

Example:

例子:

char buffer[256];
int i = 42;
sprintf(buffer, "%d", i);

(buffer will be filled with '4', '2' and trailing '\0').

(缓冲区将填充 '4'、'2' 和尾随 '\0')。

回答by nikosdi

your sprintf is wrong.You should write sprintf(string,"%d",integer); If you want to send an integer over the network and thats why you want to convert it into string have a look at htons

你的 sprintf 是错误的。你应该写 sprintf(string,"%d",integer); 如果您想通过网络发送一个整数,这就是为什么要将其转换为字符串的原因,请查看htons

with these functions you can convert an integer to network format and avoid different endianness problems! If you just want to convert it to bytes you can do something like this:

使用这些函数,您可以将整数转换为网络格式并避免不同的字节序问题!如果您只想将其转换为字节,您可以执行以下操作:

char buf[4];
memcpy(buf,&integer,4);

If you want your string to have the value of the int then you should use sprintf.

如果您希望您的字符串具有 int 的值,那么您应该使用 sprintf。