Solved

Converting TCHAR to simple string

Posted on 2004-09-20
10
1,396 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 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
 
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
Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

 
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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Basic understanding on "OO- Object Orientation" is needed for designing a logical solution to solve a problem. Basic OOAD is a prerequisite for a coder to ensure that they follow the basic design of OO. This would help developers to understand the b…
The viewer will learn how to use and create new code templates in NetBeans IDE 8.0 for Windows.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

708 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now