CString.format
previously with visual studio 2003 the following code worked great
CString str,start;
// assighn "C" to start here
str.Format("%s:",start);
this gave str tthe value "C:"
Now with visual studio 2005 beta 2, upon trying to compile
I get an error "cannot convert parameter 1 from 'const char [7]' to 'const wchar_t *'"
so I tried
CString str,start;
// assighn "C" to start here
str.Format((wchar_t *)"%s:",start);
this compiles,, however str has the value of an unreadable thing. I see three squares.
Its the same with MessageBox, except the const char[7] wants to be a LPCTSTR but all I see is squares.
Anyone know what I'm doing wrong
Try encapsulating your strings with the _T() macro, this converts the string to unicode or leaves it as ansi depending on how the project is set to compile.
If you put this it should work fine:
If you put this it should work fine:
str.Format (_T("%s:"), start);
cheers, that works for most things, but how do I convert from char [] to LPWSTR I tried the _T but I got a compiler error.
Glad I could help :)
Search MSDN for WideCharToMultiByte and MultiByteToWideChar.
However, I would suggest that you use TCHAR's instead of chars. Have a look at the tchar.h file, it contains loads of string manipulation functions that work for both UNICODE and ANSI strings. And since you're using MFC, tchar.h is already included.
Hope this helps
Search MSDN for WideCharToMultiByte and MultiByteToWideChar.
However, I would suggest that you use TCHAR's instead of chars. Have a look at the tchar.h file, it contains loads of string manipulation functions that work for both UNICODE and ANSI strings. And since you're using MFC, tchar.h is already included.
Hope this helps
ok, I'm having problems with this sttement, and rather than start a new thread, I thought I would post it here.
this is the code I wrote in visual studio 2005 beta 2.
char pathInfo[MAX_DIRECTORY_LENGTH];
DWORD ResultCode = GetCurrentDirectory(MAX_DIRECTORY_LENGTH,(LPWSTR)pathInfo);
this is the code I am using to learn this stuff, I opened it in vs 2005 and converted it.
char szPath[MAX_DIR_LENGTH];
DWORD ddwRes = GetCurrentDirectory(MAX_DIR_LENGTH, szPath);
in the 2nd piece of code szPath contains "c:\docum...
in my code pathInfo contains "c", now looking at pahinfo, I see it is actuall "c:\docu.... but with a 0 (NULL) between every character.
I put the (LPWSTR) in to make it compile. without it, it can't convert from char[1024] to LPWSTR, however the 2nd code compiles without problem.
Anyone know why my code doesn't compile, but the other one does. also how can I make it compile and return the result I want?
this is the code I wrote in visual studio 2005 beta 2.
char pathInfo[MAX_DIRECTORY_LENGTH];
DWORD ResultCode = GetCurrentDirectory(MAX_DIRECTORY_LENGTH,(LPWSTR)pathInfo);
this is the code I am using to learn this stuff, I opened it in vs 2005 and converted it.
char szPath[MAX_DIR_LENGTH];
DWORD ddwRes = GetCurrentDirectory(MAX_DIR_LENGTH, szPath);
in the 2nd piece of code szPath contains "c:\docum...
in my code pathInfo contains "c", now looking at pahinfo, I see it is actuall "c:\docu.... but with a 0 (NULL) between every character.
I put the (LPWSTR) in to make it compile. without it, it can't convert from char[1024] to LPWSTR, however the 2nd code compiles without problem.
Anyone know why my code doesn't compile, but the other one does. also how can I make it compile and return the result I want?
Like I said, TCHAR will solve all your problems! :) Well... nearly:
The whole point of the tchar.h file is to add some degree of compatibilty between ANSI and UNICODE. In the tchar.h header there will be something along the lines of:
Therefore if you always use TCHAR instead of char then, 9 times out of 10, everything should work fine. Typecasting between UNICODE and ANSI strings will very rarely work correctly because ANSI has lots of character sets whereas UNICODE just has the one. So different types are used to handle the two strings, char is 8 bits in size and wchar_t and unsigned short are 16 bits if I remember correctly.
The reason that you're getting a null after each letter is because of this size difference. For example:
As you can see, there is a 0x00 (i.e. null) either side of each character.
Try putting this:
[Edited by - DuFaceTheBass on June 4, 2005 12:10:40 PM]
The whole point of the tchar.h file is to add some degree of compatibilty between ANSI and UNICODE. In the tchar.h header there will be something along the lines of:
#ifdef UNICODE// This may be unsigned short, depending on how the compiler has been cofigured#define TCHAR wchar_t#else#define TCHAR char#endif
Therefore if you always use TCHAR instead of char then, 9 times out of 10, everything should work fine. Typecasting between UNICODE and ANSI strings will very rarely work correctly because ANSI has lots of character sets whereas UNICODE just has the one. So different types are used to handle the two strings, char is 8 bits in size and wchar_t and unsigned short are 16 bits if I remember correctly.
The reason that you're getting a null after each letter is because of this size difference. For example:
char szHello[] = _T("Hello"); // equals 0x48 0x65 0x6C 0x6C 0x6Fwchar_t wszHello[] = _T("Hello"); // equals 0x0048 0x0065 0x006C 0x006C 0x006F
As you can see, there is a 0x00 (i.e. null) either side of each character.
Try putting this:
TCHAR pathInfo[MAX_DIRECTORY_LENGTH];DWORD ResultCode = GetCurrentDirectory(MAX_DIRECTORY_LENGTH, pathInfo);
[Edited by - DuFaceTheBass on June 4, 2005 12:10:40 PM]
cheers, I'm lookking into TCHAR, but have hit another problem.
// It may not be LPTSTR but something similar (I don't have the code on me right now)
LPTSTR data = new TCHAR[1024];
data = globals::DLDiirectory.GetBuffer(1024);
globals::DLDiirectory contains "C:\documents And Settings"
after this data = "C:\Documents A"
I also tried _tcscpy(data,globals::DLDiirectory);
but got the same prob.
globals::DLDiirectory.GetLength(); returns 14 its really odd. Also the value of globals::DLDiirectory doesn't seem to affect it. It can be a string 100 characters long, and it may return the same as before or 50 characcters.
// It may not be LPTSTR but something similar (I don't have the code on me right now)
LPTSTR data = new TCHAR[1024];
data = globals::DLDiirectory.GetBuffer(1024);
globals::DLDiirectory contains "C:\documents And Settings"
after this data = "C:\Documents A"
I also tried _tcscpy(data,globals::DLDiirectory);
but got the same prob.
globals::DLDiirectory.GetLength(); returns 14 its really odd. Also the value of globals::DLDiirectory doesn't seem to affect it. It can be a string 100 characters long, and it may return the same as before or 50 characcters.
Why do you need to change it into a TCHAR? Why can't you keep it is a CString? Assuming globals::DLDiirectory is a CString.
Can you post the function where the problem is?
Can you post the function where the problem is?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement