Link to home
Start Free TrialLog in
Avatar of shragi
shragiFlag for India

asked on

typecasting in C

I need to typecast integer value into char array and char array to integer value ..
using C language ...how can I do it...

int i = 20;
char send[4] = ?         // here i must have the interger value as char...

similarly how to convert char to integer
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

You can't typecast between and array and an int but you can copy the memory between them using memcpy. Something like...

memcpy(&i, send, sizeof(int));
memcpy(send, &i, sizeof(int));

http://www.cplusplus.com/reference/clibrary/cstring/memcpy/
void * memcpy ( void * destination, const void * source, size_t num );

Note, that the result you'll get from this will be dependent upon the endianess of the platform you are using. THis being the case this code is non-portable.
http://en.wikipedia.org/wiki/Endianness

Alternatively, use a union

union myUnion
{
   int i;
   char send[4];
};

http://www.cplusplus.com/doc/tutorial/other_data_types/

Note the C standard doesn't require an int to be 4 bytes so the code examples above are non-portable. This isn't likely to be a problem if you are only targeting a single platform but it is something you need to be aware of.
>> You can't typecast between and array
Actually, just to clarify... you can but the result won't be what you are expecting. Sorry I didn't mean that statement to be misleading... what you want to do isn't typecast but to convert and the semantics and the result are different.
Avatar of shragi

ASKER

actually i need to send a char msg ....I can send only char.... so I need to convert to the int to char...

actually i need to send that integer value...

Avatar of shragi

ASKER

ya  i need to convert to char....

and later I need to covert that char to integer again...
Ok, so I've shown you how to do that above {http:#24382735} ... do you need any more clarification? Is there something that doesn't make sense that maybe I can try and explain better for you?
Avatar of shragi

ASKER

no i could get ur point...

can i able to convert int to char  in C language....
>> can i able to convert int to char  in C language

You can assign an int to a char but this will lose data (a char normally being only 1 byte and an int 4 on most 32 bit platforms) so to prevent the compiler warning you it is necessary to use a cast.

int i = <somevalue>;
char c = (char) i;
int i = 20;
char send[4] = ?

Here what u want to store?
  Here 'Send' can hold 4 chars. But u are having only one int.
  So if u want to store one int only, u can use
               send[0] = (char) i;
Else
  evilrix has already explained well how to use UNION.

If u want all field of array should contain value of i, the can use
      int i = 20;
      char arr[4] = {(char)i, (char)i,(char)i, (char)i};
      OR //char arr[4] = {i, i, i, i};
//Botth are acceptable. but problem is if value of i is greater than 255 then it'd be trucated.There is also another logic

      for(int x = 0; x<4; ++x)
            printf("%c ", arr[x]);// If u want char to be printed. If want integer to be printed u can use %i instead of %c

Actually, if u can  explain ur need completely, like waht exactly u want, it'll help us to help u in a better way.  
shragi, do you need any more information on this? If so please ask away, otherwise please don't forget to close your question when you have suitable answers.
ASKER CERTIFIED SOLUTION
Avatar of evilrix
evilrix
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial