How to convert CString to char[] in MFC?(Very Urgent............)
-
Hi All, How to conver CString to char[] in MFC. In my code: CString str = "Sample"; I need to convert to char sam[6]; Please help me........... Thanks and Regards, Anitha
-
Hi All, How to conver CString to char[] in MFC. In my code: CString str = "Sample"; I need to convert to char sam[6]; Please help me........... Thanks and Regards, Anitha
Did you see here.[^] First start searching yourself and then ask specific questions.
You need to google first, if you have "It's urgent please" mentioned in your question. ;-)_AnShUmAn_
-
Hi All, How to conver CString to char[] in MFC. In my code: CString str = "Sample"; I need to convert to char sam[6]; Please help me........... Thanks and Regards, Anitha
And see The Complete Guide to C++ Strings, Part II - String Wrapper Classes[^] for more info about strings.
Of one Essence is the human race thus has Creation put the base One Limb impacted is sufficient For all Others to feel the Mace (Saadi )
-
Hi All, How to conver CString to char[] in MFC. In my code: CString str = "Sample"; I need to convert to char sam[6]; Please help me........... Thanks and Regards, Anitha
"Very-Urgent" = "Please ignore my message". That's the meaning in forums. Anyway, a crude way:
char ch\[128\]; CString cs = "YourName"; strcpy(ch, (LPSTR)(LPCTSTR)cs);
You can also use GetBuffer, ReleaseBuffer.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
"Very-Urgent" = "Please ignore my message". That's the meaning in forums. Anyway, a crude way:
char ch\[128\]; CString cs = "YourName"; strcpy(ch, (LPSTR)(LPCTSTR)cs);
You can also use GetBuffer, ReleaseBuffer.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
I'm gonna disagree with you here VuNic. The CString has an LPCTSTR operator, which means that casting to a TCHAR* is automatic. If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly. Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
I'm gonna disagree with you here VuNic. The CString has an LPCTSTR operator, which means that casting to a TCHAR* is automatic. If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly. Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
Cedric Moonen wrote:
If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly.
No recommendations?? :)
Regards, Sandip.
SandipG wrote:
No recommendations??
Yes, try to understand what you are doing :-D . For instance by reading this excellent article[^] (but I think Hamid already provided a link to the article).
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
I'm gonna disagree with you here VuNic. The CString has an LPCTSTR operator, which means that casting to a TCHAR* is automatic. If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly. Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++lol that's why I called it a crude way. ;) But it's not going to make any terrible impact there as he's coping the buffer. But a bad practice I agree.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
Cedric Moonen wrote:
If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly.
No recommendations?? :)
Regards, Sandip.
-
lol that's why I called it a crude way. ;) But it's not going to make any terrible impact there as he's coping the buffer. But a bad practice I agree.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
VuNic wrote:
But it's not going to make any terrible impact
Yes it is: if UNICODE is enabled, then you just copy a unicode string into a non-unicode string. That's why it is much better to understand what's going, otherwise you will end up with unexpected results.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
VuNic wrote:
But it's not going to make any terrible impact
Yes it is: if UNICODE is enabled, then you just copy a unicode string into a non-unicode string. That's why it is much better to understand what's going, otherwise you will end up with unexpected results.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++When he said CString to char[] it looked apparent it's non-unicode. Otherwise he'd have used TCHARs. But as I said it's still a bad way.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
When he said CString to char[] it looked apparent it's non-unicode. Otherwise he'd have used TCHARs. But as I said it's still a bad way.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
VuNic wrote:
When he said CString to char[] it looked apparent it's non-unicode.
No, because when you never heard about UNICODE (like me when I started) and if you are using the latest visual studio versions where UNICODE is enabled by default, then you are in trouble. When I didn't understand anything about all that stuff, I was still using char* everywhere (instead of TCHAR*) and was forcing casts between everything. I can tell you that my code was a big mess X|
VuNic wrote:
Otherwise he'd have used TCHARs
TCHAR is the generic type, it is not specifically UNICODE. You should use TCHAR as often as possible... Anyway, my whole point was: do not use GetBuffer/ReleaseBuffer and explicit casts, unless you exactly know what you are doing :) .
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++ -
"Very-Urgent" = "Please ignore my message". That's the meaning in forums. Anyway, a crude way:
char ch\[128\]; CString cs = "YourName"; strcpy(ch, (LPSTR)(LPCTSTR)cs);
You can also use GetBuffer, ReleaseBuffer.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
VuNic wrote:
You can also use GetBuffer, ReleaseBuffer
These are reserved: can be used only when Mark Salsbery is logged in. :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
I'm gonna disagree with you here VuNic. The CString has an LPCTSTR operator, which means that casting to a TCHAR* is automatic. If you want to convert it explicitely to a char*, then it probably means you did something wrong with the code and you don't know how to use anycode properly. Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++Cedric Moonen wrote:
Anyway, calling GetBuffer/ReleaseBuffer is NOT something I would suggest, because as I said the CString already has an LPCTSTR operator, so the GetBuffer call returns exactly the same.
Noone should suggest
GetBuffer/ReleaseBuffer
(unless Mark is nearby...) just because of LPCTSTR operator return value isn't the same. (Carlo the Nitpick)If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
VuNic wrote:
When he said CString to char[] it looked apparent it's non-unicode.
No, because when you never heard about UNICODE (like me when I started) and if you are using the latest visual studio versions where UNICODE is enabled by default, then you are in trouble. When I didn't understand anything about all that stuff, I was still using char* everywhere (instead of TCHAR*) and was forcing casts between everything. I can tell you that my code was a big mess X|
VuNic wrote:
Otherwise he'd have used TCHARs
TCHAR is the generic type, it is not specifically UNICODE. You should use TCHAR as often as possible... Anyway, my whole point was: do not use GetBuffer/ReleaseBuffer and explicit casts, unless you exactly know what you are doing :) .
Cédric Moonen Software developer
Charting control [v1.5] OpenGL game tutorial in C++Yep that would be really a mess in particular if you had used char* specific library functions.
Cedric Moonen wrote:
Anyway, my whole point was: do not use GetBuffer/ReleaseBuffer and explicit casts, unless you exactly know what you are doing
Agreed. :). I feel lazy at times & use these crude ways but only when I'm sure nobody else handles the code.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
VuNic wrote:
You can also use GetBuffer, ReleaseBuffer
These are reserved: can be used only when Mark Salsbery is logged in. :rolleyes:
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
[My articles] -
"Very-Urgent" = "Please ignore my message". That's the meaning in forums. Anyway, a crude way:
char ch\[128\]; CString cs = "YourName"; strcpy(ch, (LPSTR)(LPCTSTR)cs);
You can also use GetBuffer, ReleaseBuffer.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
VuNic wrote:
strcpy(ch, (LPSTR)(LPCTSTR)cs);
Why the unnecessary casts?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
VuNic wrote:
strcpy(ch, (LPSTR)(LPCTSTR)cs);
Why the unnecessary casts?
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
Valid question. Actually I typed my reply with a char* . char* ch = (LPSTR)(LPCTSTR)cs; But knew that's a bad example. So changed that to char[] & and put the example as copy-the-buffer but had left the LPSTR caste unremoved . LPSTR is not need actually in this case.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
-
Valid question. Actually I typed my reply with a char* . char* ch = (LPSTR)(LPCTSTR)cs; But knew that's a bad example. So changed that to char[] & and put the example as copy-the-buffer but had left the LPSTR caste unremoved . LPSTR is not need actually in this case.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus
VuNic wrote:
LPSTR is not need actually in this case.
Neither cast is needed. Just use:
strcpy(ch, cs);
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
-
VuNic wrote:
LPSTR is not need actually in this case.
Neither cast is needed. Just use:
strcpy(ch, cs);
"Old age is like a bank account. You withdraw later in life what you have deposited along the way." - Unknown
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
:doh: Yes we've been discussing that it returns a const char by default. The reason why I used LPCTSTR is that I couldn't cast it straight to char*. As CString doesn't support non-const version. I'm stupid I didn't think before typing. So we don't need all these caste craps when you are copying the buffer.
He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus