简体   繁体   中英

How to convert char* to double?

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.

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.

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 .

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;

如果您使用强制转换,则将指针数据视为双精度值,这是错误的,因为指针是代表内存中地址的整数,您需要解释char*指向的数据以获得正确的结果,请使用sscanfatof或其他带char*double函数

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;
}

Your reinterpret_cast approach would be unlikely to work unless a floating point variable with value of -1964734.544 was literally represented in memory using the set of characters {'-', '1', '9', '6', '4', '7', '3', '4', '.', '5', '4', '4'} .

No floating point value is represented in memory that way. Period.

If you want C solutions, use functions like sscanf() , strtod() , and others. These are possible in C++ because of backward compatibility, but most of them are formally deprecated.

A C++ solution would involve using a istringstream , for example;

#include <sstream>
#include <iostream>

int main()
{
     std::istringstream s("-1964734.544");
     double x;
     if (s >> x)
        std::cout << " x = " << x << '\n';
     else
        std::cout << "Whoops!  String cannot be interpreted as floating point\n";
     return 0;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM