473,386 Members | 1,720 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,386 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 12194
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: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
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,...
0
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...

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.