468,512 Members | 1,369 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,512 developers. It's quick & easy.

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 11691
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

Post your reply

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

Similar topics

9 posts views Thread by Fausto Lopez | last post: by
8 posts views Thread by johnsto | last post: by
2 posts views Thread by Alejandro Aleman | last post: by
7 posts views Thread by nicolas.hilaire | last post: by
3 posts views Thread by fooshm | last post: by
3 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
14 posts views Thread by =?Utf-8?B?Sm9hY2hpbQ==?= | last post: by
5 posts views Thread by T. Crane | last post: by
4 posts views Thread by gw7rib | last post: by
reply views Thread by NPC403 | last post: by
1 post views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.