Rectangle 27 5

TCHAR type is char or wchar_t, depending on your project settings.

#ifdef UNICODE
     // TCHAR type is wchar_t
 #else
     // TCHAR type is char
 #endif

So if you must use std::string instead of std::wstring, you should use a converter function. I may use wcstombs or WideCharToMultiByte.

TCHAR * text;

#ifdef UNICODE
    /*/
    // Simple C
    const size_t size = ( wcslen(text) + 1 ) * sizeof(wchar_t);
    wcstombs(&buffer[0], text, size);
    std::vector<char> buffer(size);
    /*/
    // Windows API (I would use this)
    std::vector<char> buffer;
    int size = WideCharToMultiByte(CP_UTF8, 0, text, -1, NULL, 0, NULL, NULL);
    if (size > 0) {
        buffer.resize(size);
        WideCharToMultiByte(CP_UTF8, 0, text, -1, static_cast<BYTE*>(&buffer[0]), buffer.size(), NULL, NULL);
    }
    else {
        // Error handling
    }
    //*/
    std::string string(&buffer[0]);
#else
    std::string string(text);
#endif

I tried it, got: error C2664: 'std::basic_string<_Elem,_Traits,_Ax>::basic_string(const std::basic_string<_Elem,_Traits,_Ax> &)' : cannot convert parameter 1 from 'TCHAR [50]' to 'const std::basic_string<_Elem,_Traits,_Ax> &'

c++ - How to convert a TCHAR array to std::string? - Stack Overflow

c++ string unicode tchar
Rectangle 27 5

AFAIK when you work with std::wstring on Windows in C++ and store using UTF-8 in files (which sounds good and reasonable), then you have to convert the data to UTF-8 when writing to a file, and convert back to UTF-16 when reading from a file. Check out this link: Writing UTF-8 Files in C++.

I would stick with the Visual Studio default of project -> Properties -> Configuration Properties -> General -> Character Set -> Use Unicode Character Set, use the wchar_t type (i.e. with std::wstring) and not use the TCHAR type. (E.g. I would just use the wcslen version of strlen and not _tcslen.)

unicode - Confused about C++'s std::wstring, UTF-16, UTF-8 and display...

c++ unicode utf-8 utf-16 wstring
Rectangle 27 1

This example removes all non-alphanumeric and white space characters from a wide string. You can mix it up with any of the other ctype.h helper functions to remove complex-looking character-based tests.

(I'm not sure how these functions would handle CJK languages, so walk softly there.)

// Boring C loops: 'for(int i=0;i<str.size();i++)' 
    // Boring C++ eqivalent: 'for(iterator iter=c.begin; iter != c.end; ++iter)'
TSTRING label = _T("1.   Replen & Move  RPMV");
TSTRING newLabel = label;
set<TCHAR> badChars; // Use ispunct, isalpha, isdigit, et.al. (lambda version, with capture list parameter(s) example; handiest thing since sliced bread)
for_each(label.begin(), label.end(), [&badChars](TCHAR n){
    if (!isalpha(n) && !isdigit(n))
        badChars.insert(n);
});

for_each(badChars.begin(), badChars.end(), [&newLabel](TCHAR n){
    newLabel.erase(std::remove(newLabel.begin(), newLabel.end(), n), newLabel.end());
});

This is just academic, since it would clearly be more precise, concise and efficient to combine the 'if' logic from lambda0 (first for_each) into the single lambda1 (second for_each), if you have already established which characters are the "badChars".

How to remove certain characters from a string in C++? - Stack Overflo...

c++ string character
Rectangle 27 0

Here is the CPP code that duplicates _TCHAR * argv[] to char * argn[].

If you adopting old code to Windows, simple use define mentioned in the code as optional.

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

_tfopen(argv[2], TEXT("r"));
_tfopen(argv[2], L"r");

The second one will give compilation error if the macro UNICODE is not defined, that is, when TCHAR is just char, not wchar_t.

How to open a file using _TCHAR* as a file name? c/c++ - Stack Overflo...

c++ c file tchar
Rectangle 27 0

TCHAR is a Microsoft-specific typedef for either char or wchar_t (a wide character).

Conversion to char depends on which of these it actually is. If TCHAR is actually a char, then you can do a simple cast, but if it is truly a wchar_t, you'll need a routine to convert between character sets. See the function MultiByteToWideChar()

You bring up an excellent point. Although in this particular situation I think the TChar is a wide character I'll only need to do the conversion if it isn't. which I gotta check somehow.

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

The simplest way is to use the conversion macros:

we should not use these conversions, because if we call these in recursion, it won't release the memory

@Vinay Create a helper function that calls one of these, then copies the result into the heap or into another buffer. Once the helper function returns, the stack memory is released.

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

Although in this particular situation I think the TChar is a wide character I'll only need to do the conversion if it isn't. which I gotta check somehow.

if (sizeof(TCHAR) != sizeof(wchar_t))
{  .... }

The cool thing about that is both sizes of the equals are constants, which means that the compiler will handle (and remove) the if(), and if they are equal, remove everything inside the braces

we can also perform using #ifdef _UNICODE {...} #else {....} #endif

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

As the previous poster mentioned, you can use stringstreams to convert ints to strings and vise versa, but with C++11 there are some new features that eliminate the need to do this. You can use std::to_string or std::to_wstring to convert ints to strings or wstrings, and you can use c_str() to get the raw char* or wchar_t* from the string object. You should be able to convert the appropriate type of pointer to your winapi string, but some can depend on your compiler's settings.

For your reference, here's what the winapi strings mean:

LPSTR = char*
LPCSTR = const char*
LPWSTR = wchar_t*
LPCWSTR = const wchar_t*
LPTSTR = char* or wchar_t* depending on _UNICODE
LPCTSTR = const char* or const wchar_t* depending on _UNICODE

And here is a quick sample of how to assign them to C++ strings:

#include <iostream>
#include <string>
#include <Windows.h>
#include <tchar.h>

int main()
{
    // Declare winapi strings
    LPSTR str_charPtr;
    LPCSTR str_constCharPtr;
    LPWSTR str_wcharPtr;
    LPCWSTR str_constWcharPtr;
    LPTSTR str_tcharPtr;
    LPCTSTR str_constTcharPtr;

    // Declare a test integer
    int num = 5001;

    // Convert the integer to a string and to a wstring
    std::string regString = std::to_string(num);
    std::wstring wideString = std::to_wstring(num);

    // Attempt to assign the winapi strings to the C++ standard strings
    str_charPtr = const_cast<char*>(regString.c_str());         // NOTE: removing const to store in non-const LPSTR
    str_constCharPtr = regString.c_str();
    str_wcharPtr = const_cast<wchar_t*>(wideString.c_str());    // NOTE: removing const to store in non-cost LPWSTR
    str_constWcharPtr = wideString.c_str();
    str_tcharPtr = const_cast<TCHAR*>(regString.c_str());       // Error if set to Unicode
    str_tcharPtr = const_cast<TCHAR*>(wideString.c_str());      // Error if NOT set to Unicode
    str_constTcharPtr = regString.c_str();                      // Error if set to Unicode
    str_constTcharPtr = wideString.c_str();                     // Error if NOT set to Unicde

    return 0;
}

If you've moved on to Visual Studio 2012 by now, you can adjust your settings like so: 1. Right-click the project in the solution explorer. 2. Click on Properties. 3. In the left pane under Configuration Properties, select General 4. In the right pane, look in the Project Defaults section. 5. Next to Character Set, choose Unicode or whichever option is appropriate for you.

directx - Drawing value of a variable with C++ and DirectX9 - Stack Ov...

c++ directx directx-9
Rectangle 27 0

I realize this is an old thread, but it didn't get me the "right" answer, so am adding it now.

The TEXT macro will only work with string literals...

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

I read the question as stipulating that a conversion was necessary, but point taken.

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

There are a few answers in this post as well, especially if you're looking for a cross-platform solution:

How can it be crosss platform there is no TCHAR anyware else. Its windows specific.

What is the simplest way to convert char[] to/from tchar[] in C/C++(ms...

c++ c string char tchar
Rectangle 27 0

_tfopen(argv[2], TEXT("r"));
_tfopen(argv[2], L"r");

Second one is not so correct. What if TCHAR is just char? L"r" always makes wchar_t, as far as I know!

You Are right @Nawaz, but in most cases TCHAR is WCHAR and this code will work. Otherwise it will give compile error and will be corrected easily.

@Mihran: As I said, what if TCHAR is char? Your code will fail!

It will give compile error and easily can be used first one.

@Mihran: Pedanticism has nothing to do with it. You should write your code so that it functions in both cases.

How to open a file using _TCHAR* as a file name? c/c++ - Stack Overflo...

c++ c file tchar