Continue to Site

Welcome to EDAboard.com

Welcome to our site! EDAboard.com is an international Electronics Discussion Forum focused on EDA software, circuits, schematics, books, theory, papers, asic, pld, 8051, DSP, Network, RF, Analog Design, PCB, Service Manuals... and a whole lot more! To participate you need to register. Registration is free. Click here to register now.

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.

Part and Inventory Search

Welcome to EDABoard.com

Sponsor

Back
Top