? CString to char* in Unicode; How to convert?

  • Thread starter Markus Hämmerli
  • Start date
M

Markus Hämmerli

I ' ll tra to convert a Cstring to char* without success.
I working in a Unicode enabled environment

this is working in Unicode
CString source = _T("TestString");
TCHAR *szSource = source.GetBuffer(0 );

but i need a char* and so this is not working

CString source = _T("TestString");
char* mszSource = source.GetBuffer(0 );


I get the error 'system' : cannot convert parameter 1 from 'unsigned short
*' to 'const char *'

have got anybody an idea how to solve it.

Many Thanks a beginner

Markus
 
J

John Harrison

Markus Hämmerli said:
I ' ll tra to convert a Cstring to char* without success.
I working in a Unicode enabled environment

this is working in Unicode
CString source = _T("TestString");
TCHAR *szSource = source.GetBuffer(0 );

but i need a char* and so this is not working

CString source = _T("TestString");
char* mszSource = source.GetBuffer(0 );


I get the error 'system' : cannot convert parameter 1 from 'unsigned short
*' to 'const char *'

have got anybody an idea how to solve it.

Many Thanks a beginner

Markus

OK, first question, a char can hold 256 different characters (typically).
Unicode defines 60000 different characters (more or less). How do you
propose to deal with that?

There are various options depending on your situation.

john
 
P

Peter van Merkerk

I ' ll tra to convert a Cstring to char* without success.
I working in a Unicode enabled environment

this is working in Unicode
CString source = _T("TestString");
TCHAR *szSource = source.GetBuffer(0 );

but i need a char* and so this is not working

CString source = _T("TestString");
char* mszSource = source.GetBuffer(0 );

First neither CString nor the _T() macro are part of the ISO C++ standard.
Since this newsgroup only deals with standard C++ issues this question is
not really topical here. Seeing CString and _T() makes me think you are
programming for MS Windows with MFC. If that is the case look for a group
with windows and/or MFC in its name. To find out what is topical or not, and
more apporopriate alternatives for this newsgroup read this:
http://www.slack.net/~shiva/welcome.txt and this:
http://www.parashift.com/c++-faq-lite/ .
I get the error 'system' : cannot convert parameter 1 from 'unsigned short
*' to 'const char *'

The problem is that char cannot be used to store Unicode characters, it is
not big enough. You will have to select a type that can hold a Unicode
character such as wchar_t. Though on the Windows platform i.c.w. CString you
probably want to use TCHAR since like CString this one automatically
switches between char and wchar_t depending on whether you do a Unicode
build or not.

If you really need the char type, for example because you need to pass an
ASCII representation of the string to some function, then you will have to
convert the Unicode string. This may or may not be possible depending on the
contents of the Unicode string; if contains characters (e.g. chinese
characters) that are not representable in the target representation (e.g.
ASCII) then you have a problem.
 
G

Gianni Mariani

John said:
OK, first question, a char can hold 256 different characters (typically).
Unicode defines 60000 different characters (more or less). How do you
propose to deal with that?

Unicode has around 1.1 million code points (characters).

Any code that deals with only 16 bit code-points is likely not
conforming to Unicode.
 
M

Markus Hämmerli

John Harrison said:
OK, first question, a char can hold 256 different characters (typically).
Unicode defines 60000 different characters (more or less). How do you
propose to deal with that?

There are various options depending on your situation.

john

Yes , I konw , but i need in this cast only the frist 128 Characters.

markus
 
J

John Harrison

Markus Hämmerli said:
Yes , I konw , but i need in this cast only the frist 128 Characters.

markus

OK, but there is no way to perform this conversion with a cast. You must
allocate memory and copy over the characters one by one.

char* mszSource = new char [length of your source string + 1];
for ( int i = 0; i < length of your source string; ++i)
mszSource = character i from your source string
mszSource[length of your source string] = '\0';

CString is not part of standard C++, so its off topic in this group. I
assume you don't have any problems with 'length of your source string' or
'character i from your source string' but if you do the proper place to ask
about that is on a Windows programming group.

And don't forget to delete the allocated memory when you are done.

john
 
P

Peter van Merkerk

OK, first question, a char can hold 256 different characters
(typically).
Yes , I konw , but i need in this cast only the frist 128 Characters.

In that case you can probably do something like this:

// Untested code!!!
CString source = _T("TestString");
int len = source.GetLength();
char* mszSource = new char[len+1];
int i;
for(i=0; i < len; ++i)
{
mszSource = source;
}

mszSource = 0;
....
delete[] mszSource;
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top