0

I've tried two methods to convert my char* type ss to a double type. Here is my code(Compiled in VC++6.0 windows64-bit )

int main()
{
    char *ss = "-1964734.544";
    cout<<ss<<endl;
    cout<<*reinterpret_cast<double*>(ss)<<endl;
    cout<<*(double *)ss<<endl;
}

The results are:

-1964734.544
3.06123e-057
3.06123e-057

I'm not clear what is wrong and how to convert a char* to double.

hashtabe_0
  • 137
  • 2
  • 3
  • 11
  • possible duplicate of [How to convert a std::string to const char\* or char\*?](http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char) – zmo Mar 29 '15 at 11:29
  • 1
    looks like you are confusing pointers with data – Jimmy Mar 29 '15 at 11:30
  • Hello @hashtabe_0 and welcome to SO. Your question is a duplicate of http://stackoverflow.com/questions/347949/how-to-convert-a-stdstring-to-const-char-or-char – zmo Mar 29 '15 at 11:30
  • MSVC 6.0 still runs under 64-bit Windows? You really want to update to a C++ compiler (you know, one developed after C++ was first standardized). It is difficult to give an answer that will work for such an old compiler, because I don't think the usual, standard-conforming ways work with it. Or at least not all, and not the ones you'd use in C++. – Wintermute Mar 29 '15 at 11:31

5 Answers5

7

your issue here is that A CAST IS NOT A CONVERSION:

char *ss = "-1964734.544";
cout<<ss<<endl;
cout<<*reinterpret_cast<double*>(ss)<<endl;
cout<<*(double *)ss<<endl;

is that you're converting a string of characters into a number. What that means is that your memory is containing numbers being ascii values:

"-1964734.544"

is stored in memory as:

45, 49, 57, 54, 52, 55, 51, 52, 46, 53, 52, 52

which is in binary becomes:

00101101,00110001,00111001,00110110,00110100,00110111,00110011,00110100,00101110,00110101,00110100,00110100

within the memory. When converting to double, you're forcing the compiler to consider those numbers being read differently, which is following the IEEE754 way for doubles. And then 45,49,57,52 means something totally different using that encoding of numbers.

Then, considering chars are 8bits and double 32bits, after a cast your memory is then mapped the following way:

00101101001100010011100100110110,
00110100001101110011001100110100,
00101110001101010011010000110100

Then doing a "manual" interpretation to IEEE754 you get three floats:

1.0073988518377597E-11
1.7061830703823944E-7
4.120100094429091E-11

Which oddly is matching none of your values, so your memory sizes could be different, or some magic is happening during the casts.

The good way is to not reinterpret the memory, but to convert the value, and a good solution is to use strtod() from C, or stod from the standard library of C++. You'll find many ways to handle the conversion in the other answers or from the posts that duplicates this one.

If you want to have more fun with that just try floats on that webpage.

zmo
  • 24,463
  • 4
  • 54
  • 90
3

You can use strtod like this: double smth=strtod(ss,NULL,10);

It's possible to use a bit different syntax. See this for an example.

ForceBru
  • 43,482
  • 10
  • 63
  • 98
2

You should use std::stod function (C++11) with a more modern compiler

double result = std::stod(ss);

or, alternatively, use a std::stringstream from <sstream>

std::stringstream sstrm(ss);
double d;
sstrm >> d;
vsoftco
  • 55,410
  • 12
  • 139
  • 252
  • You didn't see the bit about VC++ 6.0, right? :P This would be a decent answer to a slightly different question, though. – Wintermute Mar 29 '15 at 11:35
  • @Wintermute I saw it but frankly I had no idea about the version number :) – vsoftco Mar 29 '15 at 11:35
  • VC++ 6.0 was released in 1998 (i.e., it was developed pre-standard, and that showed). Mind you, you're not alone in this, looking at the other answers. It's not something you expect to see in 2015. – Wintermute Mar 29 '15 at 11:36
-1

If You use cast then You treat pointer data as double value and this is wrong as pointer is an integer number representing the address in memory, You need to interpret data pointed to by char* to get the proper result, use sscanf, atof or other functions that take char* and give double

riodoro1
  • 1,246
  • 7
  • 14
-1

https://msdn.microsoft.com/en-us/library/aa272023%28v=vs.60%29.aspx

Use atof(const char*)

#include <iostream>
#include <cstdlib>

using namespace std;

int main()
{
    double pi;
    const char * str = "3.14";

    pi = atof(str);

    cout << pi << endl;

    return 0;
}
  • @harper did you check the link in my post? Look at the prototype for `atof` –  Mar 29 '15 at 12:53