data type conversion in c++?

Status
Not open for further replies.

asic1984

Full Member level 5
Joined
Nov 15, 2003
Messages
257
Helped
4
Reputation
8
Reaction score
1
Trophy points
1,298
Activity points
2,340
hi

how can i convert a double value like 0.0567 to an array of char value to send it using the udp protocol (using c++)

thanks too much for your help
 

this is known as typecasting
Code:
answer=(int)fltnum;
the typecast will cause the value of fltnum to be converted to an integer value before being assigned to answer.
in your case
Code:
a = (char)i; // Treat i as a char and assign it to a
 

hi

thanks for your reply ,but conversion like a = (char)i;

will not be useful in case for example of double type which is 8 bytes while char is 1 byte .....in this case i need an array to store the value in as what i think
ex: double t=0.05642 this number will be converted to a char array of 6 elements

i tried that using poiters .but it didnot work

thanks for help
 

the C standard library has functions for that:
(Don't forget #include "stdio.h" )

sprintf is the one you're looking for:
Code:
float theValueToConvert;
char buffer[128];

sprintf(buffer, "%.6f", theValueToConvert);
(with the 6 in the format string, the number of digits behind the comma)

more information:
https://www.cplusplus.com/ref/cstdio/sprintf.html

Antharax
 

    asic1984

    Points: 2
    Helpful Answer Positive Rating
Status
Not open for further replies.
Cookies are required to use this site. You must accept them to continue using the site. Learn more…