473,241 Members | 1,818 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,241 software developers and data experts.

LPCTSTR to char* in _UNICODE project

In my _UNICODE & _MBCS build project, I have LPCTSTR string (i.e.it will be wchar_t*) .The following function is not giving proper result and instead it is producing junk data.

I have confirmed it with writing the resultant data in to the file(not printing it on console).The actual requirement of this function was to convert LPCTSTR to char* since I want the resultant string in character buffer.

Can anyone please correct me what I have done wrong in it?


(e.g. val points to "c++ では、偉大な言語である。")
Expand|Select|Wrap|Line Numbers
  1. char* conversion(LPCTSTR val)
  2. {
  3.  LPCWSTR wstr = val; 
  4.  int count = wcslen(wstr);
  5.  char* c = new char[count + 1]; 
  6.  wchar_t* pwchr = const_cast<wchar_t*> (&wstr[0]);
  7.  for(int j = 0; j < count; ++j)
  8.  {
  9.     c[j] = static_cast<char> (*pwchr);   
  10.     pwchr++;
  11.  }    
  12.  c[count] = '\0';  
  13.  wchar_t *op; 
  14.  wcstombs(c,val,count);
  15.  return c;
  16. }
  17.  
Thanks in advance !
Nov 27 '07 #1
7 12176
xoinki
110 100+
hi,
you cant do this.
Expand|Select|Wrap|Line Numbers
  1. c[j] = static_cast<char> (*pwchr);
  2.  
instead use WideCharToMultiByte() function using CP_UTF8 to convert the data to utf8 and handle it properly..
remember.. wchar_t is UNICODE i.e 2 byte strings, if u r trying to get that in char means it is single byte.. so your program is not working.. if u use the above function, 2 byte string will be represented in multibyte format thereby u can copy to char * successfully.

Regards,
Xoinki
Nov 27 '07 #2
Hi xoinki,

Thanks for your immediate reply.
I changed my code but still not satisfied with the output...Instead of junk data which I got earlier, now I am getting"?????????????"
Expand|Select|Wrap|Line Numbers
  1. char* conversion(LPCTSTR val)
  2. {
  3. char *ansistr;
  4.  
  5. int lenW = _tcslen(val);
  6. int lenA = ::WideCharToMultiByte(CP_ACP, 0, val, lenW, 0, 0, NULL, NULL);
  7. if (lenA > 0)
  8. {
  9.     ansistr = new char[lenA + 1]; // allocate a final null terminator as well
  10.     WideCharToMultiByte(CP_ACP, 0, val, lenW, ansistr, lenA, NULL, NULL);
  11.     ansistr[lenA] = 0; // Set the null terminator yourself
  12. }
  13. else
  14. {
  15. AfxMessageBox(_T("Error!"));
  16. }
  17. char* c = new char[lenA + 1]; 
  18. strcpy(c,ansistr);
  19. //...use the strings, then free their memory:
  20. delete[] ansistr;
  21.  
  22.   return c;
  23. }
  24.  
Please correct me if I am wrong at any place....

Thanks
Nov 27 '07 #3
sicarie
4,677 Expert Mod 4TB
justin001-

A small aside - if you could read the box to the right when replying or creating a post, and use the code tags (also available here), it would be greatly appreciated and help your code be more readable.

Thanks!
Nov 27 '07 #4
justin001-

A small aside - if you could read the box to the right when replying or creating a post, and use the code tags (also available here), it would be greatly appreciated and help your code be more readable.

Thanks!
I understood my mistake.

Thanks!
Nov 27 '07 #5
sicarie
4,677 Expert Mod 4TB
Oh, it's not a mistake at all - it's just something that helps the people reading your posts a great deal.

Thanks!
Nov 27 '07 #6
xoinki
110 100+
hi justin,
1) plz check whether your OS supports chinese language fonts.. if it is not installed install it.
2) you are trying to convert into multibyte format, using CP_ACP.. nothing but ANSI codepage.. which does not support chinese/japanese/korean etc. you need to use CP_UTF8 instead as i suggested earlier.

Thnx and regards
xoinki
Nov 28 '07 #7
hi justin,
2) you are trying to convert into multibyte format, using CP_ACP.. nothing but ANSI codepage.. which does not support chinese/japanese/korean etc. you need to use CP_UTF8 instead as i suggested earlier.
Thnx and regards
xoinki
Hi xoinki,
You were absolutely right and even I changed it to CP_UTF8

Thank You very much, the question is resolved now.
Dec 3 '07 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

9
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,...
8
by: johnsto | last post by:
I'm really stuck - can someone help me! I've got a basic setup consisting of two things: 1. A C# web service 2. An unmanaged C++ DLL The WS is intended to call some functions in the DLL. The...
2
by: Alejandro Aleman | last post by:
Hello! i know this may be a newbie question, but i need to convert a string from System::String^ to char*, in the msdn page tells how, but i need to set to /clr:oldSyntax and i dont want it...
7
by: nicolas.hilaire | last post by:
hi all, i'm using this code to convert a String ^ in char * String ^str = "string .net"; IntPtr p = System::Runtime::InteropServices::Marshal::StringToHGlobalAnsi(str); LPCSTR str2 =...
3
by: fooshm | last post by:
Hello, I'm trying to convert a String^ to LPCTSTR. When using : LPCTSTR str = static_cast<LPCTSTR>(Marshal::StringToHGlobalAnsi(gcString).ToPointer()); in order to convert how ever I'm...
3
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
How to convert String^ (managed) to LPCTSTR (unmanaged)?
14
by: =?Utf-8?B?Sm9hY2hpbQ==?= | last post by:
I have seen the following function to convert from a System::String^ to a const wchar_t*. I would like to get a LPCTSTR and AFAIK LPCTSTR is equal to const wchar_t*. Then it should all work right?...
5
by: T. Crane | last post by:
Help! I am trying to figure this out, and I have the impression that it should be really easy, but I'm at a loss. Here's my problem: I want to take the output from const char *...
4
by: gw7rib | last post by:
I'm using a system in which TCHAR is typedef-ed to represent a character, in this case a wchar_t to hold Unicode characters, and LPCTSTR is typedef-ed to be a pointer to constant wchar_t. I presume...
0
by: fareedcanada | last post by:
Hello I am trying to split number on their count. suppose i have 121314151617 (12cnt) then number should be split like 12,13,14,15,16,17 and if 11314151617 (11cnt) then should be split like...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: Aftab Ahmad | last post by:
Hello Experts! I have written a code in MS Access for a cmd called "WhatsApp Message" to open WhatsApp using that very code but the problem is that it gives a popup message everytime I clicked on...
0
by: Aftab Ahmad | last post by:
So, I have written a code for a cmd called "Send WhatsApp Message" to open and send WhatsApp messaage. The code is given below. Dim IE As Object Set IE =...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

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.