?
Solved

Converting TCHAR to simple string

Posted on 2004-09-20
10
Medium Priority
?
1,513 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 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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
Many modern programming languages support the concept of a property -- a class member that combines characteristics of both a data member and a method.  These are sometimes called "smart fields" because you can add logic that is applied automaticall…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
The viewer will learn how to use and create keystrokes in Netbeans IDE 8.0 for Windows.

650 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