Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert double to its string representation

Status
Not open for further replies.

minex

Technical User
Nov 2, 2008
7
AU
Hi!

I would like to know what's the way to convert a double to its char[8] representation, and vice versa (Char[8] to double)

i.e EXAMPLES -> 1.3e-15 (made up that number)

In VS debugger you can cast double variables with ",s" to get their representation as string which is useful..

Note: I am not after "1.32" -> 1.32

Many Thanks,

 
Something like this?
Code:
union 
{
   double dd;
   char cc[8];
} equiv;

double x = 1.3e-15;
equiv.dd = x;
for (int ii = 0; ii < 8; ++ii)
   printf ("%02x ", equiv.cc[ii]);
 
It's interesting that a trick with union is not legal in C++ because after any assignment to a member of union all other members are undefined (see the C++ Standard;).

So OP refers to implementation-defined issues. Well, old good C (brutal force;) methods work, for example:
Code:
double x = 3.14;
char* pchar = reinterpret_cast<char*>(&x);
// that's a double as a char array via pchar pointer

memcopy(&x,"EXAMPLES",sizeof x);
cout << x << '\n';
Keep it simpler...
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top