473,695 Members | 2,421 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

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.GetBuffe r(0 );

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

CString source = _T("TestString" );
char* mszSource = source.GetBuffe r(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

Jul 19 '05 #1
6 24525

"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...
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.GetBuffe r(0 );

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

CString source = _T("TestString" );
char* mszSource = source.GetBuffe r(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
Jul 19 '05 #2
> 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.GetBuffe r(0 );

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

CString source = _T("TestString" );
char* mszSource = source.GetBuffe r(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.

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl
Jul 19 '05 #3
John Harrison wrote:
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.

Jul 19 '05 #4

"John Harrison" <jo************ *@hotmail.com> schrieb im Newsbeitrag
news:bj******** ****@ID-196037.news.uni-berlin.de...

"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...
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.GetBuffe r(0 );

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

CString source = _T("TestString" );
char* mszSource = source.GetBuffe r(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


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

markus
Jul 19 '05 #5

"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...

"John Harrison" <jo************ *@hotmail.com> schrieb im Newsbeitrag
news:bj******** ****@ID-196037.news.uni-berlin.de...

"Markus Hämmerli" <m.*********@so lnet.ch> wrote in message
news:3f******** *************** @newsspool.soln et.ch...
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.GetBuffe r(0 );

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

CString source = _T("TestString" );
char* mszSource = source.GetBuffe r(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


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[i] = 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
Jul 19 '05 #6
> > 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.


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.GetLengt h();
char* mszSource = new char[len+1];
int i;
for(i=0; i < len; ++i)
{
mszSource[i] = source[i];
}

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

--
Peter van Merkerk
peter.van.merke rk(at)dse.nl


Jul 19 '05 #7

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

Similar topics

9
12222
by: Fausto Lopez | last post by:
I'm getting the following error: 'strlen' : cannot convert parameter 1 from 'class CString' to 'const char *' when I try to compile the following code: HRESULT AnsiToUnicode(CString pszA, LPOLESTR* ppszW) { ULONG cCharacters; DWORD dwError;
5
14072
by: Tim Wong | last post by:
All: I am trying to convert a CString value to an unsigned char array. I found some code online that will allow me to compile, but when I try to print out...i get a whole mess. /*Begin Code*/ CString day("01"); unsigned char testDay;
3
9143
by: nsyforce | last post by:
What is the correct way to convert a const char* to a CString? I'm somewhat of a newbie and have tried several ways. While they all convert ok, I'm using a profiler that shows a memory leak for every option. Here's what I have tried: const char* test; test = getMyChar(); //CString myCString((LPCTSTR)test); //CString myCString(test); CString myCString = new CString(test);
3
25545
by: Morten Overgaard | last post by:
Hi How do I convert a managed System::String to a CString. I need to handle unicode characters ( Japan and Chinese ) as well as "normal" ANSI characters. I have tried the following char* p = (char*)System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(
4
7194
by: Burt Johnson | last post by:
I have a CString with a numeric string in it. In the past, I would use atoi( ) to convert to an int. That doesn't work with Unicode though. What is the proper Unicode method for converting a CString to an integer? -- - Burt Johnson MindStorm, Inc. http://www.mindstorm-inc.com/software.html
5
5379
by: Neo | last post by:
how convert CString to char *? regards, Mohammad Omer Nasir
4
11036
by: Cactus | last post by:
How to convert unsigned char* to CString: I wrote some function: u_char_ =55; u_char_ =66; u_char_ =77; .........=ii...... Convert_to_CS(u_char_);
4
10493
by: Susan Rice | last post by:
I'm new to using CString. Why won't the following compile? I'm using Microsoft Visual C++ 6.0 Line 37 which it complains about is the function: 37 CString ConvertFile(char *szFileName) I tried throwing in a bunch of #includes but didn't help. Here's the compiler errors: Compiling...
16
2818
by: Norman Diamond | last post by:
In an antique obsolete version of MFC, a CString expression could be subscripted in order to retrieve one element. Visual Studio 2005 defines CSimpleStringT::operator. At first glance it looks like it might have been intended to provide backwards compatibility for antique programs. But there seems to be no way to use it. CString s = _T("ab"); short i = 1; _TCHAR c = s;
0
8647
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9132
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
9004
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8838
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6506
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5842
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
3024
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2288
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
1986
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.