?
Solved

Converting TCHAR to simple string

Posted on 2004-09-20
10
Medium Priority
?
1,535 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
  • 5
  • 4
10 Comments
 
LVL 48

Accepted Solution

by:
AlexFM earned 500 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
Technology Partners: 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

Independent Software Vendors: 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!

Question has a verified solution.

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

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
Introduction This article is the first in a series of articles about the C/C++ Visual Studio Express debugger.  It provides a quick start guide in using the debugger. Part 2 focuses on additional topics in breakpoints.  Lastly, Part 3 focuses on th…
THe viewer will learn how to use NetBeans IDE 8.0 for Windows to perform CRUD operations on a MySql database.
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

850 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