Mail Archives: djgpp/1999/03/29/23:21:04
In article <7dk0c8$v0e$1 AT nnrp1 DOT dejanews DOT com>, ryan_brown AT my-dejanews DOT com wrote:
>How can I convert values from a variable of one type to another variable of a
>different type? I need to convert a float variable over to a char variable.
>In pascal there were functions for this in c I can't find anything. I have
>heard of casting but I can't get it to work and there isn't any documentation
>about it anywhere. Someone also told me that you can't cast between float
>and char. Does anyone know of a funciton or anything that can be used for
>this? Thanks in advance.
When you refer to a 'Char variable' do you mean a single one byte 'char',
or a string [of chars]?
Mixing variables and constants of any data type is usually permissable
without any need for casting, you can just assign them to eachother or use
them both in expressions without problem.
If you means strings, however, then there are libc functions for that:
for string to numeric variable:
int i;
float f;
i=atoi("344"); //A[scii] to I[nteger]
f=atof("32.2"); //A[scii] to F[loat]
for numeric to string:
char buffer[100];
buffer=itoa(i); //I[nteger] to A[scii]
buffer=ftoa(f); //F[loat] to A[scii]
There are some other functions with can be a little more versitile, like
strtol, which can handle different number systems other than decimal.
- Raw text -