473,385 Members | 1,615 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

Double -> C-String conversion

During every iteration in a loop, I need to convert a double value into a
char*. Simple type-casting did not work, so I thought of using
stringstreams. However, the stream would have to be *emptied* after each
iteration (and I could not conceive of a practical way doing that).

Cheers,
Matthias
--
Für emails Anweisung in der Adresse befolgen
Jul 22 '05 #1
16 4851
Der Andere wrote:
During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.
Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.
However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).


Do the following:

thestream.str("");

Jul 22 '05 #2
Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a
char*. Simple type-casting did not work, so I thought of using
stringstreams. However, the stream would have to be *emptied* after each
iteration (and I could not conceive of a practical way doing that).
How about using sprintf ?

sprintf(p, "%lf", doubleval);
// Remember to allocate memory to p before you do this.

HTH
Cheers,
Matthias
--
Für emails Anweisung in der Adresse befolgen

--
Karthik

------

Human Beings please 'removeme' for my email.
Jul 22 '05 #3
Rolf Magnus wrote:
Der Andere wrote:

During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.

Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.

However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).

Do the following:

thestream.str("");


Alternatively:

thestream.str( ).clear( );
Jul 22 '05 #4
> > During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.


Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.


C-string, yes. I thought I could indicate a c-string with char *: Would
char[] be better?
However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).


Do the following:

thestream.str("");


It works now, cheers :-)

Matthias
Jul 22 '05 #5
Jeff Schwab wrote:
Rolf Magnus wrote:
Der Andere wrote:

During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.

Sounds as if you want a string representation of your double. A char*
is not a string. It's a pointer to char, nothing less, nothing more.

However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).

Do the following:

thestream.str("");


Alternatively:

thestream.str( ).clear( );


According to Stroustrup, this shouldn't empty the stream, because str()
returns a copy of the string.

Jul 22 '05 #6
Der Andere wrote:
> During every iteration in a loop, I need to convert a double value
> into a char*. Simple type-casting did not work, so I thought of
> using stringstreams.


Sounds as if you want a string representation of your double. A char*
is not a string. It's a pointer to char, nothing less, nothing more.


C-string, yes. I thought I could indicate a c-string with char *:
Would char[] be better?


No. Actually, char[] is only allowed if used as a paremter. I just
wanted to clarify that char* isn't C's string type. C doesn't have a
string type. Therefore, there is no built-in way to convert something
into a string, and that explains why casting to char* doesn't do what
you want.

Jul 22 '05 #7
On Sun, 25 Apr 2004 16:37:02 -0700, Karthik
<re*******************@yahoo.com> wrote in comp.lang.c++:
Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a
char*. Simple type-casting did not work, so I thought of using
stringstreams. However, the stream would have to be *emptied* after each
iteration (and I could not conceive of a practical way doing that).
How about using sprintf ?


That's a possibility but...
sprintf(p, "%lf", doubleval);
In all versions of the ISO C standard prior to 1999, including the
1995 version that the C++ standard inherits from, "%lf" is an
ill-formed conversion specifier for the *printf() functions and
produces undefined behavior.

There is no need for separate conversion specifiers for float and
double in *printf, as floats are always promoted to double as is the
case for all optional parameters to all variadic functions in C and
C++.

The 1999 update to the C standard made the 'l' length modifier a no-op
with the all floating point conversion specifiers, but this is not
part of the current C++ standard. The "%lf" *printf conversion
specifier causes undefined behavior in C++.
// Remember to allocate memory to p before you do this.

HTH

Cheers,
Matthias


--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #8

"Jeff Schwab" <je******@comcast.net> wrote in message
news:3o********************@comcast.com...
Rolf Magnus wrote:
Der Andere wrote:

During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.

Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.

However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).

Do the following:

thestream.str("");


Alternatively:

thestream.str( ).clear( );


That doesn't work. It clears the error flags of the stream. That's what
clear does for any stream, it has nothing to do with emptying a string
stream.

john
Jul 22 '05 #9

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:sg********************************@4ax.com...
On Sun, 25 Apr 2004 16:37:02 -0700, Karthik
<re*******************@yahoo.com> wrote in comp.lang.c++:
Der Andere wrote:
During every iteration in a loop, I need to convert a double value into a char*. Simple type-casting did not work, so I thought of using
stringstreams. However, the stream would have to be *emptied* after each iteration (and I could not conceive of a practical way doing that).


How about using sprintf ?


That's a possibility but...
sprintf(p, "%lf", doubleval);


In all versions of the ISO C standard prior to 1999, including the
1995 version that the C++ standard inherits from, "%lf" is an
ill-formed conversion specifier for the *printf() functions and
produces undefined behavior.


How did this misunderstanding become so widespread? So widespread in fact
that the C standard committee felt obliged to change the standard.

john
Jul 22 '05 #10

Rolf Magnus <ra******@t-online.de> wrote in message
news:c6*************@news.t-online.com...
No. Actually, char[] is only allowed if used as a paremter. I just
wanted to clarify that char* isn't C's string type. C doesn't have a
string type. Therefore, there is no built-in way to convert something
into a string, and that explains why casting to char* doesn't do what
you want.

Nor does C++ have a string type. It has a library template instantiation
called. C has a string concept, it is not an intrinsic type just as C++'s is
not. That C string, and you will find the term defined in the C standard, is
an array or array-like region of char terminated by a null character.

To convert a double to such a string, the sprintf() function can be used. I
would recommend using C++ strstreams instead.
Brian Rodenborn
Jul 22 '05 #11
"John Harrison" <jo*************@hotmail.com> wrote in message
news:c6************@ID-196037.news.uni-berlin.de...
sprintf(p, "%lf", doubleval);


In all versions of the ISO C standard prior to 1999, including the
1995 version that the C++ standard inherits from, "%lf" is an
ill-formed conversion specifier for the *printf() functions and
produces undefined behavior.


How did this misunderstanding become so widespread? So widespread in fact
that the C standard committee felt obliged to change the standard.


Because when you want to read a double value, you use:
sscanf(p, "%lf", &doubleval); // for float: ... "%f", &floatval)

The implicit conversion of variadic function parameters from
float to double does not apply to pointers, as used in scan functions.

Given that the implicit conversion exists in printf (the parameters
received are always double, never float), it kind of makes sense
that %f and %lf be made equivalent, or even that %lf be the primary
choice. (But as Jack pointed out, %lf is formally UB in C++98 and C90).
Regards,
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form
Jul 22 '05 #12
Rolf Magnus wrote:
Jeff Schwab wrote:

Rolf Magnus wrote:
Der Andere wrote:

During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.
Sounds as if you want a string representation of your double. A char*
is not a string. It's a pointer to char, nothing less, nothing more.

However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).
Do the following:

thestream.str("");


Alternatively:

thestream.str( ).clear( );

According to Stroustrup, this shouldn't empty the stream, because str()
returns a copy of the string.


Whoops! You're absolutely right.
Jul 22 '05 #13
John Harrison wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:3o********************@comcast.com...
Rolf Magnus wrote:
Der Andere wrote:

During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.
Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.

However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).
Do the following:

thestream.str("");


Alternatively:

thestream.str( ).clear( );

That doesn't work. It clears the error flags of the stream. That's what
clear does for any stream, it has nothing to do with emptying a string
stream.


No, John, you've mistaken the clear() method of the stream for the
clear() method of the underlying stream. The str() method here returns
a string, not a stream.

This is a great example of why unnecessary abbreviations are such a bad
idea when choosing identifiers.
Jul 22 '05 #14
John Harrison wrote:
"Jeff Schwab" <je******@comcast.net> wrote in message
news:3o********************@comcast.com...
Rolf Magnus wrote:
Der Andere wrote:

During every iteration in a loop, I need to convert a double value
into a char*. Simple type-casting did not work, so I thought of using
stringstreams.
Sounds as if you want a string representation of your double. A char* is
not a string. It's a pointer to char, nothing less, nothing more.

However, the stream would have to be *emptied* after each iteration
(and I could not conceive of a practical way doing that).
Do the following:

thestream.str("");


Alternatively:

thestream.str( ).clear( );

That doesn't work. It clears the error flags of the stream. That's what
clear does for any stream, it has nothing to do with emptying a string
stream.

john


No, John, you're wrong. You've mistaken the clear() method of the
stream for the clear() method of the underlying stream. The str()
method here returns a string, not a stream. This does clear the string
representation of the stream's contents, not the stream's error flags.
(Unfortunately, as Rolf pointed out, it clears only a temporary copy of
the string, so the stream is unaffected. I wonder, why isnt the
temporary string const?)

This is a great example of why unnecessary abbreviations are such a bad
idea when choosing identifiers.
Jul 22 '05 #15
On Mon, 26 Apr 2004 06:13:44 +0100, "John Harrison"
<jo*************@hotmail.com> wrote in comp.lang.c++:

"Jack Klein" <ja*******@spamcop.net> wrote in message
news:sg********************************@4ax.com...
On Sun, 25 Apr 2004 16:37:02 -0700, Karthik
<re*******************@yahoo.com> wrote in comp.lang.c++:
Der Andere wrote:

> During every iteration in a loop, I need to convert a double value into a > char*. Simple type-casting did not work, so I thought of using
> stringstreams. However, the stream would have to be *emptied* after each > iteration (and I could not conceive of a practical way doing that).

How about using sprintf ?


That's a possibility but...
sprintf(p, "%lf", doubleval);


In all versions of the ISO C standard prior to 1999, including the
1995 version that the C++ standard inherits from, "%lf" is an
ill-formed conversion specifier for the *printf() functions and
produces undefined behavior.


How did this misunderstanding become so widespread? So widespread in fact
that the C standard committee felt obliged to change the standard.

john


How did "void main()" become so widespread, as it is considerably more
so than "%lf"?

The real problem is the lack of symmetry between the *printf and
*scanf families. Both "%f" and "%lf" are required for the *scanf
family of functions, which receive a pointer to the destination type
and therefore can convert to both float and double.

Given that, it would have been better to accept "%lf" in printf all
along, and many compilers do as an extension, but there are also some
(no idea on relative percentage) that choke on it, as the standard
entitles them to do.

--
Jack Klein
Home: http://JK-Technology.Com
FAQs for
comp.lang.c http://www.eskimo.com/~scs/C-faq/top.html
comp.lang.c++ http://www.parashift.com/c++-faq-lite/
alt.comp.lang.learn.c-c++
http://www.contrib.andrew.cmu.edu/~a...FAQ-acllc.html
Jul 22 '05 #16
"Der Andere" <ma**************@gmx.net> wrote in message news:<c6*************@news.t-online.com>...
During every iteration in a loop, I need to convert a double value into a
char*. Simple type-casting did not work, so I thought of using
stringstreams. However, the stream would have to be *emptied* after each
iteration (and I could not conceive of a practical way doing that).


I'm not sure you need to "empty" the stream after each iteration,
maybe just construct a new one.

Maybe like this
----------------------------
#include <iostream>
#include <sstream>
int main() {
static const double x[] = { 0.5, 123.456789, -99.2 };
for(int i=0; i<sizeof(x)/sizeof(x[0]); i++) {
std::ostringstream ox;
ox << x[i];
std::cout
<< i << " " // number of item
<< x[i] << " " // raw item
<< ox.str() << " " // item as string
<< ox.str().c_str() // item as char *
<< std::endl;
}
return 0;
}
----------------------------
The output is:

0 0.5 0.5 0.5
1 123.457 123.457 123.457
2 -99.2 -99.2 -99.2

HTH
LR
Jul 22 '05 #17

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Pepijn Kenter | last post by:
Dear experts. I have a vector<float> and want to convert that to a vector<double>. I optimistically tried: #include <vector> #include <iostream> using namespace std; int main() {
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
31
by: Bjørn Augestad | last post by:
Below is a program which converts a double to an integer in two different ways, giving me two different values for the int. The basic expression is 1.0 / (1.0 * 365.0) which should be 365, but one...
0
by: npotnis | last post by:
Hi All, I am trying to compile an existing C++ unamanaged application with the /clr switch in VS.Net 2003, as I need to use managed extensions in the code. In a cpp file I have the following...
11
by: Bo Peng | last post by:
Dear C++ experts, I need to store and retrieve a meta information that can be int or double. The program would be significantly simpler if I can handle two types uniformly. Right now, I am using...
11
by: Ole Nielsby | last post by:
First, sorry if this is off-topic, not strictly being a C++ issue. I could not find a ng on numerics or serialization and I figure this ng is the closest I can get. Now the question: I want...
3
by: J.M. | last post by:
I have data in a double array of length 2N, which actually represents complex numbers with real and imaginary parts interlaced. In other words, elements in this array with even indices represents...
1
by: perroe | last post by:
Hi I have a array of complex numbers that are stored in a simple double array. This is done since the array is part of an wrapper for an external C library, and the imaginary part of the first...
11
by: Steven Woody | last post by:
long i = nnn; long j; double d; d = i; j = ( long )d; in this case, i == j ? thanks.
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.