14

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.

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

colithium
  • 10,269
  • 5
  • 42
  • 57
Waypoint
  • 17,283
  • 39
  • 116
  • 170
  • 1
    I do not see anything "parallel programming" about this. Please explain? – abelenky Feb 15 '11 at 19:20
  • My app is first in parallel programming course, hence there is no tag to Parallel programing, just have some problems with casting to char a appending chars – Waypoint Feb 15 '11 at 20:32

5 Answers5

16

You can use itoa function to convert the integer to a string.

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

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 unsigned in this case (though you may still lose data).

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

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

Edit

Go here for 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/

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.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
MD Sayem Ahmed
  • 28,628
  • 27
  • 111
  • 178
  • 1
    Note that using `strcat` is always a bad idea. Even its safer versions (`strncat`, `strlcat`) perform poorly; n insertions will cost O(n^2) since each call will need to traverse the string to find the end. If you're doing lots of concatenation it's best to track the current size in a separate variable. – asveikau Feb 15 '11 at 19:30
4

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

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 strcat or sprintf.

Jason Plank
  • 2,336
  • 5
  • 31
  • 40
littleadv
  • 20,100
  • 2
  • 36
  • 50
  • This particular example is also very likely to cause undefined behavior on many platforms (where `char` is signed and cannot hold values up to 65535). – eq- Feb 15 '11 at 19:29
3

Casting int to char involves losing data and the compiler will probably warn you.

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

number & 0x000000ff; /* first byte */
(number & 0x0000ff00) >> 8; /* second byte */
(number & 0x00ff0000) >> 16; /* third byte */
(number & 0xff000000) >> 24; /* fourth byte */
Blagovest Buyukliev
  • 42,498
  • 14
  • 94
  • 130
0
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.

Andrew
  • 24,218
  • 13
  • 61
  • 90
  • C leaves it to the implementation to specify whether `char` is `signed` or `unsigned`. In practice compilers tend to follow whatever the architecture's ABI recommends, so `char` is signed on x86 and unsigned on PPC/ARM. GCC can be overridden with `-fsigned-char` and `-funsigned-char`, but this potentially breaks ABI with installed libraries. – ephemient Feb 15 '11 at 19:20
  • @ephemient: did not know. Thanks! – Andrew Feb 15 '11 at 19:23
0
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.

abelenky
  • 63,815
  • 23
  • 109
  • 159