Solved

Converting TCHAR to simple string

Posted on 2004-09-20
10
1,443 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
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!

 
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

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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
C++ Language error 28 281
NOTEPAD++ Meaning of (+) and (-) Icons 6 164
c++ using a LPCOLESTR * and adding a string varaible to the Pointer 2 76
eclipse apache tomcat admin console 52 156
When writing generic code, using template meta-programming techniques, it is sometimes useful to know if a type is convertible to another type. A good example of when this might be is if you are writing diagnostic instrumentation for code to generat…
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…
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

726 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