Casting a char array to a double
-
I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle
-
I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle
If you mean you have a double in the first 8 bytes of the array then you can get at it like this: double dbl = *((double*)cBuffer); or alternatively if the bytes that contain the double start say 6 bytes into the array then the syntax is nearly the same but you need the address of the first byte of the double: double dbl = *((double*)&cBuffer[5]); Just one thing to be careful of is that x86 cpu's allow this casting from anywhere in memory but some cpu's have strict rules about things being aligned on even byte boundaries so if portability is important then you may need to copy the bytes using memcpy() or something first to ensure the proper alignment. Matt
-
I have a char array defined as char cBuffer[200] and I want to convert this to a double. Can anyone help with the syntax to do this? Thanks, Kyle
Casting it to double? Or converting the contents to double? Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
Casting it to double? Or converting the contents to double? Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
kyledunn wrote: Converting the contents to double describes it well. Then the solution is elementary. Use
atof(...)
double num = atof(buffer);
Hope that helped, Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.
-
kyledunn wrote: Converting the contents to double describes it well. Then the solution is elementary. Use
atof(...)
double num = atof(buffer);
Hope that helped, Nish
Check out last week's Code Project posting stats presentation from :- http://www.busterboy.org/codeproject/ Feel free to make your comments.