Omgili, forum search, forums search, search forums, discussion search,discussions search, search discussions, board search, boards search, search boards
  Advanced Search

[Boost-users] Conversion from numeric type to const char*

On Mon, 30 Jan 2012 05:17:06 -0800 (PST), Claude <...@tiscali.it

What is the better way for convert a numeric type into a const char *?
I use:

(boost::lexical_cast<std::string
It is a good idea?

--
View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-char-t p4340944p4340944.html
Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users



On Mon, 30 Jan 2012 14:55:31 +0100, Roman Perepelitsa <...@gmail.com

2012/1/30 Claude <...@tiscali.it

Depends how you use the value.

// Safe.
puts(boost::lexical_cast<std::string
const char* s = boost::lexical_cast<std::string // Don't do this! Here 's' is a dangling pointer; the string has already
// been destroyed.
puts(s);

Roman Perepelitsa.

<div class="gmail_quote"
What is the better way for convert a numeric type into a const char *?<br I use:<br<br        (boost::lexical_cast<std::string>(myNumericVar)).c_str()<br<brIt is a good idea?</blockquote
<div
<div

_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 30 Jan 2012 06:00:48 -0800 (PST), Claude <...@tiscali.it

Ok. I could use strcpy() to copy the .c_str() result in my char array?

--
View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-char-t p4340944p4341054.html
Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 30 Jan 2012 11:27:16 -0500, Jeff Flinn <...@gmail.com

If you absolutely need to fill a char array use std::string's copy
method for that:

char buf[100];

boost::lexical_cast<std::string
See Josutti's The C++ Standard Library 11.2.4

Alternatively boost::iostreams::array_sink to directly stream to a char
buffer.

Jeff

_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 30 Jan 2012 06:00:47 -0800 (PST), Claude <...@tiscali.it

Ok. I could use strcpy() to copy the .c_str() result in my char array?

--
View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-char-t p4340944p4341053.html
Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 30 Jan 2012 15:05:19 +0100, Roman Perepelitsa <...@gmail.com

2012/1/30 Claude <...@tiscali.it

You could, although it's usually better to keep std::string and let it care
about memory management. When you need to pass const char* to a C function
(e.g., puts), use c_str().

Roman Perepelitsa.

<div class="gmail_quote"
Ok. I could use strcpy() to copy the .c_str() result in my char array?</blockquote
<div

_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 30 Jan 2012 08:40:29 -0500, Brian Allison <...@gmail.com

On Mon, Jan 30, 2012 at 8:17 AM, Claude <...@tiscali.it
Using a *const char ** is prone to Undefined Behaviors. If there's a way to
use *std::string *instead, then try to do that.

But - if you *must *use a *const char ** [say for a 3rd party interface
that requires it] then that's as good as any method.

Be aware of the potential for an exception, and be ready to deal with it.

<div class="gmail_quote"What is the better way for convert a numeric type into a const char *?<br I use:<br<br        (boost::lexical_cast<std::string>(myNumericVar)).c_str()<br<brIt is a good idea?<br<font color="#888888"<br

_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users

On Mon, 30 Jan 2012 05:51:10 -0800 (PST), Claude <...@tiscali.it

I would like to convert it into const char * why should use it with a C
function...

--
View this message in context: http://boost.2283326.n4.nabble.com/Conversion-from-numeric-type-to-const-char-t p4340944p4341030.html
Sent from the Boost - Users mailing list archive at Nabble.com.
_______________________________________________
Boost-users mailing list
Boos...@lists.boost.org
http://lists.boost.org/mailman/listinfo.cgi/boost-users