Solved

typecasting in C

Posted on 2009-05-14
12
949 Views
Last Modified: 2012-05-07
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
0
Comment
Question by:shragi
  • 6
  • 3
12 Comments
 
LVL 40

Expert Comment

by:evilrix
ID: 24382735
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.
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24382752
>> 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.
0
 

Author Comment

by:shragi
ID: 24382759
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...

0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 

Author Comment

by:shragi
ID: 24382764
ya  i need to convert to char....

and later I need to covert that char to integer again...
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24382784
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?
0
 

Author Comment

by:shragi
ID: 24382896
no i could get ur point...

can i able to convert int to char  in C language....
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24383033
>> 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;
0
 
LVL 8

Expert Comment

by:Subrat (C++ windows/Linux)
ID: 24392315
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.  
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24393787
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.
0
 
LVL 40

Accepted Solution

by:
evilrix earned 250 total points
ID: 24708845
>> Please make your recommendations here

The question was, "I need to typecast integer value into char array and char array to integer value .."

http:#24392315 doesn't provide a solution to this. It suggests casting an int type into *each* char within the array, which will not achieve what the asker is asking for and will result in data loss as I explain here http:#24382752 and here http:#24383033...

int i = 4321;
char send[4] = {(char)i, (char)i,(char)i, (char)i};

Will result in 'send' being as follows...

send[0] == -31; // in most cases char is a signed type (but doesn't have to be!)
send[1] == -31;
send[2] == -31;
send[3] == -31;

And there is no way to revert back to the original int value.

This is not serialising an int into a 4 char array, to do that you need to perform a bitwize copy, I explain here http:#24382735

memcpy(&i, send, sizeof(int)); // bitwize copy if int into 4 char array
memcpy(send, &i, sizeof(int)); // bitwize copy of 4 char array to int

It should also be noted that a char doesn't have to be the size of a byte (although I have yet to see a platform where it's not) and int doesn't have to be 4 bytes in size.

I'd like to think that I've already providing working solution for the asker; however, I am happy to expand further if more information is required.
0

Featured Post

Simplifying Server Workload Migrations

This use case outlines the migration challenges that organizations face and how the Acronis AnyData Engine supports physical-to-physical (P2P), physical-to-virtual (P2V), virtual to physical (V2P), and cross-virtual (V2V) migration scenarios to address these challenges.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
why "." vs "->" 23 120
Grammars for C C++ and java 1 122
Assigning default value to structure in C for mutithread application 17 66
A  &&  B  || (C||D) how it works in c 7 56
An Outlet in Cocoa is a persistent reference to a GUI control; it connects a property (a variable) to a control.  For example, it is common to create an Outlet for the text field GUI control and change the text that appears in this field via that Ou…
Summary: This tutorial covers some basics of pointer, pointer arithmetic and function pointer. What is a pointer: A pointer is a variable which holds an address. This address might be address of another variable/address of devices/address of fu…
The goal of this video is to provide viewers with basic examples to understand and use structures in the C programming language.
The goal of this video is to provide viewers with basic examples to understand how to use strings and some functions related to them in the C programming language.

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question