Solved

Converting TCHAR to simple string

Posted on 2004-09-20
10
1,464 Views
Last Modified: 2013-12-14
Hi,

How do I convert a TCHAR string to a simple string?

I'm trying to do a comparision with some std::string functions, but i keep getting an error during compilation which says that could not convert TCHAR string to simple string or something...

Regards,
Kelvin
0
Comment
Question by:wiredx
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 125 total points
ID: 12101959
#include <atlbase.h>


void PrintTCHAR(TCHAR* s)
{
    USES_CONVERSION;

    printf(T2CA(s));            // T2CA converts TCHAR* to const char*
}

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vclib/html/_atl_String_Conversion_Macros.asp
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/vcmfc98/html/_atl_string_conversion_macros.asp
0
 
LVL 7

Expert Comment

by:jimwasson
ID: 12101961
A TCHAR will resolve to whatever the project is built as -- either MBCS or unicode.
"std::string str" will use ascii chars. You might try using a wide string:
    std::wstring str;
0
 

Author Comment

by:wiredx
ID: 12102015
AlexFM: sorry for the newbie question.. so how do i use that code on top.. to say convert  a TCHAR called "blah"?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 48

Expert Comment

by:AlexFM
ID: 12102057
#include <atlbase.h>

USES_CONVERSION;

TCHAR* s = _T("blah");    

if ( strcmp(T2CA(s), "blah") == 0 )    // strcmp requires const char*
    ...
0
 

Author Comment

by:wiredx
ID: 12110499
hmm.. strange. i still get this error:

error C2664: 'strcmp' : cannot convert parameter 2 from 'class tcharcls' to 'const char *'
        No user-defined-conversion operator available that can perform this conversion, or the operator cannot be called
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12110773
Something wrong with second parameter. Please show your code.
0
 

Author Comment

by:wiredx
ID: 12110873
AlexFM:

i think the code is too much to post here... i zipped it up at http://blog.com.sg/code.zip
the stuff is in src.cpp, and what im trying to do is to convert the "btemp" and "temp" values from there so that i could manipulate some strings as shown as in.. http://www.devx.com/getHelpOn/10MinuteSolution/16972/0/page/3

sorry for the trouble AlexFM, but im really new and struggling with c++ :(
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12111018
2.27 MB is too much for download. Can you post some code fragment or give zip file of acceptable size?
0
 

Author Comment

by:wiredx
ID: 12111039
oops. sorry, forgot to Clean the temp files.
its now 28.4 KB... --> http://blog.com.sg/code2.zip
0
 
LVL 48

Expert Comment

by:AlexFM
ID: 12111265
This is not UNICODE-ANSI strings problem. Your project is not UNICODE, all strings are ANSI strings. TCHAR is char. class tcharcls in your project is char* wrapper. I don't know why do you use this class instead of std::string, but this is your choice.
The problem is that class tcharcls doesn't have TCHAR* casting operator, this is a reason why some lines of your code are not compiled.
You don't need all string conversion stuff from my first post. Make the following change in your code:

TCHAR* s = btemp.getValue();

if ( strcmp(s, btemp.getValue()) == 0 )
{
    cout << s << endl;
}

If class tcharcls doesn't know to convert itself to TCHAR*, use it's getValue function when you need to get TCHAR* pointer from tcharcls instance.
0

Featured Post

On Demand Webinar: Networking for the Cloud Era

Did you know SD-WANs can improve network connectivity? Check out this webinar to learn how an SD-WAN simplified, one-click tool can help you migrate and manage data in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
The viewer will learn how to use NetBeans IDE 8.0 for Windows to connect to a MySQL database. Open Services Panel: Create a new connection using New Connection Wizard: Create a test database called eetutorial: Create a new test tabel called ee…
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question