C语言 C - 将 int 转换为 char 并将 char 附加到 char

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5008394/
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 07:48:57  来源:igfitidea点击:

C - casting int to char and append char to char

c

提问by Waypoint

I am making my first parallel application, but I am stuck with basics of C. I need to know, how to cast int to char and then how to append one char to another.

我正在制作我的第一个并行应用程序,但我坚持使用 C 的基础知识。我需要知道,如何将 int 转换为 char,然后如何将一个 char 附加到另一个。

It you could help me please, i would be glad. Thank you.

如果你能帮助我,我会很高兴。谢谢你。

回答by MD Sayem Ahmed

You can use itoafunction to convert the integer to a string.

您可以使用itoa函数将整数转换为字符串。

You can use strcatfunction to append characters in a string at the end of another string.

您可以使用strcat函数将字符串中的字符附加到另一个字符串的末尾。

If you want to convert a integer to a character, just do the following -

如果要将整数转换为字符,只需执行以下操作 -

int a = 65;
char c = (char) a;

Note that since characters are smaller in size than integer, this casting may cause a loss of data. It's better to declare the character variable as unsignedin this case (though you may still lose data).

请注意,由于字符的大小小于整数,因此此转换可能会导致数据丢失。unsigned在这种情况下最好声明字符变量(尽管您可能仍然会丢失数据)。

To do a light reading about type conversion, go here.

要轻松阅读有关类型转换的内容,请转到此处

If you are still having trouble, comment on this answer.

如果您仍然遇到问题,请对此答案发表评论。

Edit

编辑

Go herefor a more suitable example of joining characters.

这里寻找一个更合适的加入角色的例子。

Also some more useful link is given below -

下面还提供了一些更有用的链接-

  1. http://www.cplusplus.com/reference/clibrary/cstring/strncat/
  2. http://www.cplusplus.com/reference/clibrary/cstring/strcat/
  1. http://www.cplusplus.com/reference/clibrary/cstring/strncat/
  2. http://www.cplusplus.com/reference/clibrary/cstring/strcat/

Second Edit

第二次编辑

char msg[200];
int msgLength;
char rankString[200];

........... // Your message has arrived
msgLength = strlen(msg);
itoa(rank, rankString, 10); // I have assumed rank is the integer variable containing the rank id

strncat( msg, rankString, (200 - msgLength) );  // msg now contains previous msg + id

// You may loose some portion of id if message length + id string length is greater than 200

Third Edit

第三次编辑

Go to this link. Here you will find an implementation of itoa. Use that instead.

转到此链接。在这里,您将找到itoa. 改用那个。

回答by littleadv

Casting int to char is done simply by assigning with the type in parenthesis:

将 int 转换为 char 只需通过括号中的类型赋值即可完成:

int i = 65535;
char c = (char)i;

Note: I thought that you might be losing data (as in the example), because the type sizes are different.

注意:我认为您可能会丢失数据(如示例中所示),因为类型大小不同。

Appending characters to characters cannot be done (unless you mean arithmetics, then it's simple operators). You need to use strings, AKA arrays of characters, and <string.h>functions like strcator sprintf.

无法将字符附加到字符(除非您的意思是算术,否则它是简单的运算符)。您需要使用字符串、AKA 字符数组以及<string.h>类似strcator 的函数sprintf

回答by Blagovest Buyukliev

Casting intto charinvolves losing data and the compiler will probably warn you.

转换intchar涉及丢失数据,编译器可能会警告您。

Extracting a particular byte from an intsounds more reasonable and can be done like this:

int声音中提取特定字节听起来更合理,可以这样做:

number & 0x000000ff; /* first byte */
(number & 0x0000ff00) >> 8; /* second byte */
(number & 0x00ff0000) >> 16; /* third byte */
(number & 0xff000000) >> 24; /* fourth byte */

回答by abelenky

int myInt = 65;

char myChar = (char)myInt;  // myChar should now be the letter A

char[20] myString = {0}; // make an empty string.

myString[0] = myChar;
myString[1] = myChar; // Now myString is "AA"

This should all be found in any intro to C book, or by some basic online searching.

这应该可以在任何 C 入门书或一些基本的在线搜索中找到。

回答by Andrew

int i = 100;
char c = (char)i;

There is no way to append one char to another. But you can create an array of chars and use it.

无法将一个字符附加到另一个字符。但是您可以创建一个字符数组并使用它。