简体   繁体   English

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

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

I want to know the method of converting an integer into char/string and vice-versa also.我想知道将 integer 转换为 char/string 的方法,反之亦然。

I have already used sprintf(&charvar,"%d",&intvar) but it produces wrong output, possibly garbage.我已经使用了 sprintf(&charvar,"%d",&intvar) 但它产生了错误的 output,可能是垃圾。

i have also heard atoi() in gcc has bugs.Reference: GCC atoi bug我还听说 gcc 中的 atoi() 有错误。参考: GCC atoi bug

What is the other method to convert string/char back to int?将字符串/字符转换回 int 的另一种方法是什么?

Actually i want to send an integer from one machine to another using SOCK_STREAM.实际上,我想使用 SOCK_STREAM 将 integer 从一台机器发送到另一台机器。

//EDIT: I forgot to tell that sprintf() does conversion and returns positive value. //编辑:我忘了告诉 sprintf() 进行转换并返回正值。

Remove the ampersand before intvar :删除intvar之前的 & 符号:

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

Two notes:两个注意事项:

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

Here's some example code:这是一些示例代码:

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

If you want to send an integer to another machine you can send it as binary data, just by sending the intvar directly to the stream, you don't have to convert it to a char first.如果您想将 integer 发送到另一台机器,您可以将其作为二进制数据发送,只需将intvar直接发送到 stream,您不必先将其转换为 char。 That will only introduce problems with knowing the length of the data as different values generate different lengths of strings.这只会引入知道数据长度的问题,因为不同的值会生成不同长度的字符串。

Please read the manual of 'sprintf' and 'sscanf', and maybe their safer versions are proper for you.请阅读 'sprintf' 和 'sscanf' 的手册,也许他们更安全的版本更适合你。

You cannot sprintf to a variable.你不能sprintf到一个变量。 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.此外,参数应该是 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')。

your sprintf is wrong.You should write sprintf(string,"%d",integer);你的 sprintf 是错误的。你应该写 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如果您想通过网络发送 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:使用这些功能,您可以将 integer 转换为网络格式并避免不同的字节顺序问题:如果您只想将其转换为字节,您可以执行以下操作:

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。

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

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