Discussion:
how can I convert it from cstring to char
(too old to reply)
terrcy
2004-04-16 10:02:01 UTC
Permalink
to All:
I have the function:
myfun(unsigned char * buf)
and I define a cstring variable str:
cstring str;
Question:
how can I convert the variable and after pass it into my function

here ,I do it but error.

char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?

appreciated for anyone of reply

best regards
terrcy
2004-04-16 10:03:00 UTC
Permalink
Post by terrcy
myfun(unsigned char * buf)
cstring str;
how can I convert the variable and after pass it into my function
here ,I do it but error.
char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?
appreciated for anyone of reply
best regards
Balboos
2004-04-16 11:07:11 UTC
Permalink
To stay in a mode similar to what you've tried:

char *ch;
CString cstr;

ch = strdup((LPCSTR)cstr);

which will later require you to free(ch);

But, you should be OK just casting your string:

myfun((unsigned char *)(LPCSTR)cstr);

This can be more conveniently handled by creating one or more overloads
of myfun() that do your conversion(s) and then pass the string through
to myfun()

As always, probably a dozen other solutions can be suggested.

balboos
Post by terrcy
myfun(unsigned char * buf)
cstring str;
how can I convert the variable and after pass it into my function
here ,I do it but error.
char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?
appreciated for anyone of reply
best regards
HF
2004-04-16 11:17:44 UTC
Permalink
Post by terrcy
myfun(unsigned char * buf)
cstring str;
how can I convert the variable and after pass it into my function
here ,I do it but error.
char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?
How about

char ch* = str.GetBuffer(0); // Retrieves a pointer to the internal
// character buffer for the
CString
// object.
myfun(ch);

?

Remember if you change the string contents using the pointer returned by
GetBuffer, then you should call str.ReleaseBuffer before using any other
CString methods.


- HF
Tom Serface
2004-04-17 01:07:49 UTC
Permalink
Why not make the function a (const char *) then you can simply pass the
CString to it.

For example:

void myfun(LPCTSTR var)
...

CString str;
str = "test"

myfun(str);

CString will automatically cast itself to the LPCTSTR.

Tom
Post by terrcy
myfun(unsigned char * buf)
cstring str;
how can I convert the variable and after pass it into my function
here ,I do it but error.
char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?
appreciated for anyone of reply
best regards
terrcy
2004-04-21 01:16:09 UTC
Permalink
because I mustdefine a unsigned char *
Post by Tom Serface
Why not make the function a (const char *) then you can simply pass the
CString to it.
void myfun(LPCTSTR var)
...
CString str;
str = "test"
myfun(str);
CString will automatically cast itself to the LPCTSTR.
Tom
Post by terrcy
myfun(unsigned char * buf)
cstring str;
how can I convert the variable and after pass it into my function
here ,I do it but error.
char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?
appreciated for anyone of reply
best regards
Eric
2004-04-23 09:52:19 UTC
Permalink
Post by terrcy
because I mustdefine a unsigned char *
Seen on MSDN:

char *s = new char[string.GetLength()+1];
strcpy(s, string);

with CString string and char *s !
Perry S. Morris
2004-04-23 14:03:25 UTC
Permalink
If you must use a unsigned char* ( which is also known as a BYTE* or
LPBYTE), you can just double cast a CString. For example: unsigned char*
ptr = (unsigned char*)((LPCTSTR)strMyString)); then just pass that.
Post by terrcy
because I mustdefine a unsigned char *
Post by Tom Serface
Why not make the function a (const char *) then you can simply pass the
CString to it.
void myfun(LPCTSTR var)
...
CString str;
str = "test"
myfun(str);
CString will automatically cast itself to the LPCTSTR.
Tom
Post by terrcy
myfun(unsigned char * buf)
cstring str;
how can I convert the variable and after pass it into my function
here ,I do it but error.
char * ch=(LPCTSTR)CString
May i use the function : const_cast ?
and how can i use it ?
appreciated for anyone of reply
best regards
Loading...