Convert double to Char Array C++

13,772

Solution 1

You are translating the literal bytes of your double into chars. The double value is a binary representation (usually something like IEEE 754) while a char is a binary representation of a character (usually something based on ASCII). These two are not compatible.

Unfortunately, this means that you must do some kind of conversion process. Either the std::to_string() that you said you don't want to do, or a more complicated std::stringbuf (which will call std::to_string() under the hood anyway)

Solution 2

You are copying a double (which is a binary representation for a number) into a char array; there is no reason those bytes should correspond to digit characters.

Solution 3

This isn’t tested but you could try:

std::string to_string(double x)
{
    std::ostringstream ss;
    ss << x;
    return ss.str();
}

You could then put the returned string’s characters into a char array like this:

std::string str = to_string(doubleValue);
char digits[str.length()];

And then, thanks to the comment from Remy Lebeau, you can do either this:

std::strncpy(digits, to_string(doubleValue).c_str(), sizeof(digits))

or this:

to_string(doubleValue).copy(digits, sizeof(digits))

Solution 4

Just do this first:

std::ostringstream strs;
strs << dbl;
std::string str = strs.str();

This converts a double dbl to a string str.

Then you can convert it to an array of chars like this:

char *cstr = new char[str.length()+1];
str.copy(cstr, str.length());
cstr[str.length()] = '\0';
// use cstr as needed...
delete[] cstr;

Or, simply this:

const char *cstr = str.c_str();
// use cstr as needed...

Solution 5

If you want to convert by hand without using to_string, you're going to have to pull digits out one at a time. A double isn't stored in memory as a set of characters, so you can't just copy over the memory and hope it all works. (for more on how doubles are interpreted, see here).

Share:
13,772

Related videos on Youtube

Delrog
Author by

Delrog

I am a student in New York and am currently studying Computer Science as my major.

Updated on October 31, 2022

Comments

  • Delrog
    Delrog over 1 year

    I'm trying to convert (and round) a double to a char array without converting with a std::to_string on the double first. However, I'm receiving random memory text instead. What am I doing wrong?

    Here is my code:

    double d = 1.0929998; 
    d = std::round(d * 100) / 100;
    
    char s[sizeof(d)];
    std::memcpy(s,&d,sizeof(d));
    

    Result:

    s: q=×£pñ?

    Intended value:

    s: 1.09

    • Pete Becker
      Pete Becker almost 6 years
      A double is not an array of char. If it was that simple there would be no need for std::to_string. There's a lot of work involved in converting a floating-point value to a text string. For a somewhat simpler problem, try converting an int value into a string. Once you've got that working you can start to think about floating-point conversions. Or you can just use the built-in library facilities. The last time I wrote a floating-point to text conversion function it took several weeks. This is not a beginner's task.
    • ShadowRanger
      ShadowRanger
      If std::to_string is so abhorrent, perhaps C APIs like sprintf would do what you need?
  • scohe001
    scohe001 almost 6 years
    How is this different from using std::to_string like OP mentioned in the question?
  • user253751
    user253751 almost 6 years
    @Delrog I'd suggest using snprintf instead of strncpy+to_string
  • Remy Lebeau
    Remy Lebeau almost 6 years
    You could just let the STL do the rounding for you. Using a std::ostringstream, look at the std::fixed and std::setprecision() stream manipulators, eg: ostringstream oss; oss << fixed << setprecision(3) << d; string s = oss.str();
  • Remy Lebeau
    Remy Lebeau almost 6 years
    That is a buffer overflow waiting to happen. At least use std::setprecision() to control how many digits the ostringstream outputs
  • Remy Lebeau
    Remy Lebeau almost 6 years
    Your edits are still risky. std::strcpy() does not do any bounds checking. At least use std::strncpy() or std::string::copy() instead, so you can specify the max size of the char[] buffer, eg: std::strncpy(digits, to_string(doubleValue).c_str(), sizeof(digits)) or to_string(doubleValue).copy(digits, sizeof(digits))
  • 0xCursor
    0xCursor almost 6 years
    @Remy Lebeau Alright, I will change that, thanks for pointing it out.
  • Sebastian D'Agostino
    Sebastian D'Agostino almost 6 years
    @scohe001 to_string is C++11. Older compilers will not support it, unlike stringstream.
  • Delrog
    Delrog almost 6 years
    Thanks, I usually try to steer clear from stringstreams though because I assume that they come with overhead. Do they? Is there a way to determine the memory/ performance cost of Standard library functions at a (cppreference or alternative docu) glance?
  • Studocwho
    Studocwho about 5 years
    It'll be useful for not just the OP but future Googlers if you explain why each set of code solves the problem and indeed the differences between each code, eg. performance, ease of use, etc.