converting TCHAR to string
Question
Monday, October 24, 2011 6:34 PM
hello
i am using Api that have varable of type TCHAR* and i what to save it to varabile i try many typs and casting but it did not work hope any one can help me what the varible i can use to store value of type TCHAR*
thanks in advance
All replies (9)
Tuesday, October 25, 2011 7:32 PM ✅Answered
Hi Aisha,
Basically when you use TCHAR you don't need to bother about multybyte or unicode.
It will be selected by TCHAR.h as per the project settings(unicode defined or not)
TCHAR is a typedef to wchar if UNICODE defined otherwise its a typedef to char
#ifdef
UNICODE
#ifndef
_TCHAR_DEFINED
typedef
WCHAR TCHAR, *PTCHAR;
typedef
WCHAR TBYTE , *PTBYTE ;
#define
_TCHAR_DEFINED
#endif
In your case you can store it in CString itself like the following way
TCHAR* pt = _T("xxxx");
CString str(pt);
Hope you got the answer
Thursday, October 27, 2011 7:10 AM ✅Answered
Hi,
Welcome to the MSDN Forum.
TCHAR is just a typedef that depending on your compilation configuration(whether you have defined UNICODE or not) defaults to char or wchar. So depending on your compilation configuration, you can convert TCHAR* to string or wstring.
To use UNICODE character set, click Project->Properties->Configuration Properties->General->Character Set, and then select "Use Unicode Character Set".
More about how to convert various Visual C++ strings into other strings, you can refer to this link in the MSDN Library: http://msdn.microsoft.com/en-us/library/ms235631.aspx. There are samples demonstrates how-to-convert.
But I have to say that the information you provided us is not enough at present for us to fully try the conversion. Please copy more codes that were related to the conversion and paste them here for our further analysis, so we can solve the issue as soon as possible.
Have a nice day.
Best regards,
Helen Zhao[MSFT]
MSDN Community Support | Feedback to us
Get or Request Code Sample from Microsoft
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Thursday, October 27, 2011 6:34 PM ✅Answered
Hi Aisha,
I have used the following code to use CString in a console application.you have to include<atlstr.h>
#include
"stdafx.h"
#include <atlstr.h>
int _tmain(int argc, _TCHAR* argv[]) {
TCHAR* st = _T("Hello");
CString str(st);
return 0;
}
Hope this will works for you.
Thanks,
Subhash
Monday, October 24, 2011 7:39 PM | 1 vote
Check http://msdn.microsoft.com/en-us/magazine/cc188714.aspx on the subject. The following is signature, not part of post
Please mark the post answered your question as the answer, and mark other helpful posts as helpful, so they will appear differently to other users who are visiting your thread for the same problem.
Visual C++ MVP
Monday, October 24, 2011 7:43 PM
i am using Api that have varable of type TCHAR* and i what to save it to varabile i try many typs and casting but it did not work hope any one can help me what the varible i can use to store value of type TCHAR*
See this thread for some explanation: http://social.msdn.microsoft.com/Forums/en-US/vcgeneral/thread/d752f215-e89f-4c87-acdc-7cf6e9e0e705/
TCHAR is either char (for Multi-Byte) or wchar (if Unicode is set). To work in both cases you can use the advice given in the thread above defining your own tstring. Like this for Unicode:
#include <string>
#include <iostream>
typedef std::basic_string<TCHAR, std::char_traits<TCHAR>> tstring;
...
tstring strString(_T(""));
TCHAR* ptrString =_T("wchar string");
strString = ptrString;
std::wcout<<strString<<"\n";
strString = tstring(_T("concatenated strings: string + "))+ptrString;
std::wcout<<strString<<"\n";
Tuesday, October 25, 2011 6:21 AM
thanks Sergey Chepurin
there is error
First-chance exception at 0x00d12081 in locatingTag.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x77ea15ee (ntdll.dll) in locatingTag.exe: 0xC0000005: Access violation reading location 0x00000000.
but if i want it array to store many varible of type TCHAR *>its array of char
Tuesday, October 25, 2011 8:52 AM
You seems to be in the learning curve...
TCHAR depends on tchar.h and also on the fact your project setting is unicode or not.
There are multiple solutions to handle that. MSDN covers is here: http://msdn.microsoft.com/en-us/library/c426s321%28v=VS.100%29.aspx
Christophe Pichaud
Tuesday, October 25, 2011 10:09 AM
thanks Sergey Chepurin
there is error
First-chance exception at 0x00d12081 in locatingTag.exe: 0xC0000005: Access violation reading location 0x00000000.
Unhandled exception at 0x77ea15ee (ntdll.dll) in locatingTag.exe: 0xC0000005: Access violation reading location 0x00000000.but if i want it array to store many varible of type TCHAR *>its array of char
I am sorry, but then there is something wrong with settings of your VC++ project. This code works (if without wcout or cout) in Multi-Byte or Unicode character set without changes. That is what TCHAR and _T macro are for.
Thursday, October 27, 2011 11:04 AM
thanks Subhash Mankunnu but i am using vc++2010 and the CString type is undefine i incloude many headr but it did not work hope you can help me