简体   繁体   中英

Only first character is assigned converting LPCTSTR to char*

I'm completely new to C++. In my program there's a function which has to take a LPCTSTR as a parameter. I want to convert it into a char* . What I tried is as follows,

char* GetChar(LPCTSTR var){
   char* id = (char*)var;
   .....
}

But while debugging I noticed that only first letter of var is assigned to id .
What have I done wrong?
(I tried various answers in StackOverflow about converting LPCTSTR to char* before coming to this solution. None of them worked for me.)
UPDATE
What i want is to get full string pointed by var to be treated as char*

You code has told the compiler to convert var (which is a pointer) into a pointer to a character and then assign that converted value to id . The only thing it converts is the pointer value. It doesn't make any changes to the thing var points to, copy it, or convert it. So you haven't done anything to the string var points to.

It's not clear what you're trying to do. But your code doesn't really do anything but convert a pointer value without changing or affecting the thing pointed to in any way.

When you convert a LPCTSTR (a long pointer to a const tchar string) to a char* , you get a char* that points to a CTSTR (a const tchar string). What use is that? What sense does that make?

Most probaby LPCTSTR is const wchar_t* , so if you cast it to char* (which is Undefined Behaviour - as var could point to literal), the LSB byte (wchar_t under Visual Studio is 16bits) of *var is zero so it is treated as '\\0' - which indicates end of string. So in the end you get only one char.

To convert LPCTSTR to char* you can use wsctombs for example, see here: Convert const wchar_t* to const char* .

Here's an easy solution I found based on other answers given here.

char* GetChar(LPCTSTR var){
   char id[30];
   int i = 0;
   while (var[i] != '\0')
   {
      id[i] = (char)var[i];
      i++;
   }
   id[i] = '\0';

UPDATE
As mentioned in comments this is not a good way to solve this problem. But if someone has the same problem and cannot understand any other solution, this will help a bit.
Therefore I won't remove this answer.

It is much more useful to just pick a character set ( wchar_t , or char ), and just stick to it, in your application, since trying to use TCHAR , when trying to support both, may cause you some headaches. To be fair, today, you can just, safely, use wchar_t (or WCHAR , since from the current types you are using, I suspect that you are using Windows headers).

The problem that you have, is because casting a pointer does not have any impact on its contents. And, since, typically wchar_t is 2 bytes in size, while char is 1 byte in size, storing the value, that fits inside a char , in wchar_t , leaves 2nd byte of wchar_t set to \\0 . And when you try to print null( \\0 )-terminated string of wchar_t s as a string of char s, the printing function reaches the \\0 character after reading the first symbol, and assumes it is the end of the string. \\0 character in wchar_t is 2 bytes long.

For example, the string

LPCWSTR test = L"Hi!";

is stored in memory as:

48 00 69 00 21 00 00 00

If you want to convert between the wchar_t version of the string to char version, or vice-versa, there exist some functions, that can do the conversion, and since I noticed that you probably are using Windows headers (from LPCTSTR define), those functions are WideCharToMultiByte / MultiByteToWideChar .

You may now start to think: I am not using wchar_t ! I am using TCHAR !

Typically TCHAR is defined in the following way:

#ifdef UNICODE
    typedef WCHAR TCHAR;
#else
    typedef char TCHAR;
#endif

So you could do similar handling in your conversion code:

template<int N>
bool GetChar(LPCTSTR var, char (&out)[N])){
    #ifdef UNICODE
        return WideCharToMultiByte (CP_ACP, 0, var, -1, out, N, NULL, NULL) != 0;
    #else
        return strcpy_s (out, var) == 0;
    #endif
}

Note, the return value of GetChar function is true if the function Succeeds; false - otherwise.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM