Search code examples
c++floating-pointhexnotationscanf

How to convert the string "0xAp-2" to double of 2.5, using a std library function


I have a string containing "0xAp-2". I want to convert that to a double value:

#include <iostream>
#include <cstdio>

int main()
{
    char array[] = "0xAp-2";

    double a;

    sscanf(array, "%lf", &a);

    std::cout << a << "\n";
}

I've tried both atof and sscanf, and can't get it to work. I apologize if the answer is obvious, but I've already tried searching google with terms such as "sscanf" and "floating hexadecimal".


Solution

  • I believe the strtod function should be able to parse binary scientific notation.