Convert double to Char Array C++
Solution 1
You are translating the literal bytes of your double
into char
s. 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).
Related videos on Youtube
Delrog
I am a student in New York and am currently studying Computer Science as my major.
Updated on October 31, 2022Comments
-
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 almost 6 yearsA
double
is not an array ofchar
. If it was that simple there would be no need forstd::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 anint
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. -
ShadowRangerIf
std::to_string
is so abhorrent, perhaps C APIs likesprintf
would do what you need?
-
-
scohe001 almost 6 yearsHow is this different from using
std::to_string
like OP mentioned in the question? -
user253751 almost 6 years@Delrog I'd suggest using
snprintf
instead ofstrncpy
+to_string
-
Remy Lebeau almost 6 yearsYou could just let the STL do the rounding for you. Using a
std::ostringstream
, look at thestd::fixed
andstd::setprecision()
stream manipulators, eg:ostringstream oss; oss << fixed << setprecision(3) << d; string s = oss.str();
-
Remy Lebeau almost 6 yearsThat is a buffer overflow waiting to happen. At least use
std::setprecision()
to control how many digits theostringstream
outputs -
Remy Lebeau almost 6 yearsYour edits are still risky.
std::strcpy()
does not do any bounds checking. At least usestd::strncpy()
orstd::string::copy()
instead, so you can specify the max size of thechar[]
buffer, eg:std::strncpy(digits, to_string(doubleValue).c_str(), sizeof(digits))
orto_string(doubleValue).copy(digits, sizeof(digits))
-
0xCursor almost 6 years@Remy Lebeau Alright, I will change that, thanks for pointing it out.
-
Sebastian D'Agostino almost 6 years@scohe001 to_string is C++11. Older compilers will not support it, unlike stringstream.
-
Delrog almost 6 yearsThanks, 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 about 5 yearsIt'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.