473,320 Members | 2,020 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,320 software developers and data experts.

std::string to char *

Hi,
how do I convert std::string to char * ?

regards,

-Ab.
Mar 6 '06 #1
5 1540
Hi,
how do I convert std::string to char * ?


you can use PtrToStringChars
because all managed strings are in unicode, this will give you a unicode
string. you can then go from unicode to ansi if you like.

see MSDN for more details. it has an example that describes how to do this.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 6 '06 #2
> Hi,
how do I convert std::string to char * ?


Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello");

const char * ptr = myString.c_str();

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 6 '06 #3
> remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not


"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:CA**********************************@microsof t.com...
Hi,
how do I convert std::string to char * ?


Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello");

const char * ptr = myString.c_str();

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 6 '06 #4
> const char * ptr = myString.c_str();

And I wont have to worry about "delete"ing the ptr right?

-Ab.
"Bruno van Dooren" <br**********************@hotmail.com> wrote in message
news:CA**********************************@microsof t.com...
Hi,
how do I convert std::string to char * ?


Oops.
previous reply was for managed strings.
for std::string you can just use the c_str() method.

std::string myString("Hello");

const char * ptr = myString.c_str();

remember that you are not supposed to use this pointer for changing
myString, and the pointer itself is only valid as long as myString is not
modified in any way.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 6 '06 #5
> > const char * ptr = myString.c_str();

And I wont have to worry about "delete"ing the ptr right?


no. definitly not. the buffer that is returned is managed by the string
object itself.

That is also the reason why you shouldn't use it after the string changes.
The standard says that that pointer is only valid while the string stays the
same.

--

Kind regards,
Bruno.
br**********************@hotmail.com
Remove only "_nos_pam"

Mar 6 '06 #6

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

Similar topics

19
by: Espen Ruud Schultz | last post by:
Lets say I have a char pointer and an std::string. Is it possible to get a pointer to the std::string's "content" so that the char pointer can point to the same text? And vice versa; can I give...
24
by: Julie | last post by:
I'm re-evaluating the way that I convert from a std::string to char *. (Requirement: the source is a std::string, the usable contents are char *) Here is what I've come up with: #include...
3
by: Heiko Hund | last post by:
Hi, I do not understand the deeper reason for the following compiler error $ g++ test.cpp test.cpp: In function `int main()': test.cpp:41: error: `std::basic_string<char,...
16
by: Khuong Dinh Pham | last post by:
I have the contents of an image of type std::string. How can I make a CxImage object with this type. The parameters to CxImage is: CxImage(byte* data, DWORD size) Thx in advance
6
by: Khuong Dinh Pham | last post by:
How do i read the contents of a xml file and output it to a std::string. Thx in advance
8
by: Jason Heyes | last post by:
If s is a std::string, does &s refer to the contiguous block of characters representing s?
37
by: jortizclaver | last post by:
Hi, I'm about to develop a new framework for my corporative applications and my first decision point is what kind of strings to use: std::string or classical C char*. Performance in my system...
10
by: sposes | last post by:
Im very much a newbie but perhaps somehone can help me. Ive been searching for a way to convert a std::string to a unsigned char* The situation is I have a function that wants a unsigned char*...
6
by: SteelSide | last post by:
Ive searched and searched,but havent been able to get it to work. ----------------- #include <iostream> using namespace std; std::string tempHostNameStr = "s1e2.hidden.thingy.org"; char...
13
by: arnuld | last post by:
/* C++ Primer 4/e * section 3.2 - String Standard Library * exercise 3.10 * STATEMENT * write a programme to strip the punctation from the string. */ #include <iostream> #include...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.