Welcome to Dream.In.Code
Getting C++ Help is Easy!

Join 86,248 C++ Programmers. There are 2,214 online right now! Ask your question and get quick answers from Dream.In.Code experts. Join the #1 programming help community on the internet! Registration is fast and FREE... Join Now!

Chat LIVE With a C++ Expert
Powered by LivePerson.com

Register to Make This Box Go Away!

Can someone help me on converting from TCHAR to CHAR?

2 Pages V  1 2 >  
Reply to this topicStart new topic

Can someone help me on converting from TCHAR to CHAR?, I found some resources online, but something about CT2A, or T2A.

turtleC++
post 8 May, 2008 - 10:05 AM
Post #1


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 13



I really want to convert from TCHAR to CHAR and the reason of that is because I need to write the data back to the file, but the data is written into the file producing extra space between letter which I believe TCHAR is the cause, so can I convert back to CHAR?

Some source codes mention about using memset() function and CT2A(), but when I use CT2A() it produces error due to the without declaring identifier. Maybe I'm missing the header file for CT2A? But I went to search for it, and I didn't see anyting about its header file.

any idea?
User is offlineProfile CardPM
Go to the top of the page
+Quote Post


Jingle
post 8 May, 2008 - 02:34 PM
Post #2


D.I.C Head

**
Joined: 20 Oct, 2007
Posts: 240

if you could post the code it would be very helpfull.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

skaoth
post 8 May, 2008 - 03:09 PM
Post #3


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 269

You can use the ATL/MFC Conversion macros like you want
but you actually have to put USE_CONVERSION in your code before actually
using the macro

CODE

void Convert(TCHAR *t)
{
USE_CONVERSION;

char *s = T2A(t);
....
}


You'll need to include AtlBase.h, AtlConv.h
Here is the documentation
http://msdn.microsoft.com/en-us/library/87zae4a3(VS.80).aspx
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

turtleC++
post 8 May, 2008 - 03:38 PM
Post #4


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 13

This is my code, please take a look.

CODE

#include "stdafx.h"
#include <Windows.h>
#include <string>


int WINAPI WinMain(    HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow)
{
    
        
    DWORD byteWritten;
    int countChar = 0;
    CHAR carriageRet1 = '\r';
    CHAR carriageRet2 = '\n';
    LPCWSTR fName = L"myFile.txt";

    HANDLE hFile = CreateFile(fName, GENERIC_READ|GENERIC_WRITE,
        FILE_SHARE_WRITE|FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);

    if(hFile == INVALID_HANDLE_VALUE)
    {
    
        MessageBox(NULL, TEXT("No myFile.txt file!"), TEXT("Window Message Error"), MB_OK);
    
    }
    else //File exists, then stamp the time.
    {

        //Count how many charaters in label.
        CHAR label[] = "EXE File Time Stamp: ";
        while(label[countChar] != '\0')
            countChar++;
        
        SYSTEMTIME st;
        GetLocalTime(&st);
        int hours = st.wHour;
        int minutes = st.wMinute;
    
        //Hours will return in military time 0-24 hours.
        if(hours > 12)
        {
            hours = hours - 12;
        }

        DWORD minuteSeconds = minutes * 60;
        DWORD hourSeconds = hours * 3600;
        DWORD totalSeconds = minuteSeconds + hourSeconds;

        TCHAR szBuffHour[256];
        TCHAR szBuffMinute[256];
        TCHAR szBuffTotalSeconds[256];

        CHAR timeColon = ':';

        swprintf(szBuffHour, _T("%d"), hours);
        swprintf(szBuffMinute, _T("%d"), minutes);
        swprintf(szBuffTotalSeconds, _T("%d"), totalSeconds);
        
        LPSTR charString = new char[_tcslen(szBuffHour) +1];
        memset(charString, 0x00, _tcslen(szBuffHour) + 1);

        strcpy(charString, CT2A(szBuffHour));

        SetFilePointer(hFile, 0, NULL, FILE_END);
        WriteFile(hFile, label, countChar, &byteWritten, NULL);
        WriteFile(hFile, &szBuffHour, 3, &byteWritten, NULL);
        WriteFile(hFile, &timeColon, sizeof(timeColon), &byteWritten, NULL);
        WriteFile(hFile, &szBuffMinute, 3, &byteWritten, NULL);

        WriteFile(hFile, &carriageRet1, sizeof(carriageRet1), &byteWritten, NULL); //add new line
        WriteFile(hFile, &carriageRet2, sizeof(carriageRet2), &byteWritten, NULL); //add new line

    }


    return 0;
}


As you can on my code, I tried to use the memset() and strcpy with CT2A(), but it doesn't work since the error is about CT2A(), so I have no idea then.

The same happens for T2A, but by the way I'm running on WinCE.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

skaoth
post 8 May, 2008 - 04:37 PM
Post #5


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 269

You have been provided the information regarding the macro CT2A(). You do not need to use memset with this macro. Please read the documentation on how to use the macros.

As I stated you need to include atlbase.h and atlconv.h into your program. CT2A is defined in atlconv.h. Since you are using WinCE I don't know if this is available to you or not. You may still be able to then anyways by including the headers and telling your compiler where those headers are.
If you can't include these headers for some reason, just find the header anyway and copy/paste their macro into your program

MSDN
QUOTE

When using an ATL string conversion macro, specify the USES_CONVERSION macro at the beginning of your function in order to avoid compiler errors. For example:

Copy Codevoid func( LPSTR lpsz )
{
USES_CONVERSION;
...
LPWSTR x = A2W(lpsz)
// Do something with x
...
}
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

turtleC++
post 8 May, 2008 - 05:19 PM
Post #6


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 13

I just add the USES_CONVERSION at the beginning WinMain, but it still has error.

USES_CONVERSION has error of undeclared identifier

do I need to include anything into my project? If not, then why I have error though.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

skaoth
post 8 May, 2008 - 05:41 PM
Post #7


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 269

please re-read my previous post. It tells you which headers you need to include.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

turtleC++
post 8 May, 2008 - 06:32 PM
Post #8


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 13

I comment out the code block where it does the coversion, and compile it with the new added header file, but it has error in the atlconv.h file saying _ASSERTE is undeclared identifier. But this time the error is not from my programing though.

This post has been edited by turtleC++: 8 May, 2008 - 06:38 PM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

Cerolobo
post 8 May, 2008 - 07:41 PM
Post #9


D.I.C Head

**
Joined: 5 Apr, 2008
Posts: 151

There are several ways to correct this, but the easiest way is to just ignore the Microsoft crap, and use standard C.

CODE

        char szBuffHour[256];  // <-- Changed TCHAR to char
        char szBuffMinute[256];
        char szBuffTotalSeconds[256];

        char timeColon = ':'; // <-- Changed CHAR to char

          // Changed swprintf to sprintf and removed _T()
        sprintf(szBuffHour, "%d", hours);
        sprintf(szBuffMinute, "%d", minutes);
        sprintf(szBuffTotalSeconds, "%d", totalSeconds);

          // Changed LPSTR to char * and changed _tcslen() to strlen()
        char *charString = new char[strlen(szBuffHour) +1];
        memset(charString, 0x00, strlen(szBuffHour) + 1);

          // Removed CT2A()
        strcpy(charString, szBuffHour);


In the above code, I removed all of Microsoft's crappy defines, and replaced them with standard C. Since you only want to deal with ASCII, there is no point using Unicode.

Another way to correct this, is to just write your own CT2A() function, which isn't hard to do.


On a side note, I would highly recommend you don't use MS defines for basic types when ever possible. By using them, you are basically forcing anyone who uses your code to use the Microsoft compiler, which is not the best thing to do.

Here are a few of the common ones
CHAR - char
LONG - long
INT - int
SHORT - short
DWORD - short
WORD - char
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

turtleC++
post 9 May, 2008 - 09:25 AM
Post #10


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 13

QUOTE(Cerolobo @ 8 May, 2008 - 07:41 PM) *

There are several ways to correct this, but the easiest way is to just ignore the Microsoft crap, and use standard C.

CODE

        char szBuffHour[256];  // <-- Changed TCHAR to char
        char szBuffMinute[256];
        char szBuffTotalSeconds[256];

        char timeColon = ':'; // <-- Changed CHAR to char

          // Changed swprintf to sprintf and removed _T()
        sprintf(szBuffHour, "%d", hours);
        sprintf(szBuffMinute, "%d", minutes);
        sprintf(szBuffTotalSeconds, "%d", totalSeconds);

          // Changed LPSTR to char * and changed _tcslen() to strlen()
        char *charString = new char[strlen(szBuffHour) +1];
        memset(charString, 0x00, strlen(szBuffHour) + 1);

          // Removed CT2A()
        strcpy(charString, szBuffHour);


In the above code, I removed all of Microsoft's crappy defines, and replaced them with standard C. Since you only want to deal with ASCII, there is no point using Unicode.

Another way to correct this, is to just write your own CT2A() function, which isn't hard to do.


On a side note, I would highly recommend you don't use MS defines for basic types when ever possible. By using them, you are basically forcing anyone who uses your code to use the Microsoft compiler, which is not the best thing to do.

Here are a few of the common ones
CHAR - char
LONG - long
INT - int
SHORT - short
DWORD - short
WORD - char


Thank for following up this problem with me.

In the previous message you mentioned about reading the macro, is it the macro that you can define on top of your source code where it acts like a normal function, but not the same as normal function-more like an inline function, is that the macro you were talking about? If that is the macro, then I sortly know that concept.

Sometimes I wonder too about big cap letter and small cap letter of the identifier. People interchange use CHAR then char, and TCHAR. I'm getting confused since I'm new to win32. So from the message above, CHAR is the same as char?

For DWORD, is it character type or integer type? because I remember I can assign integer to it. Since it has the word WORD at the end of it, so I assume it's character type, right?

thanks.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

mikeblas
post 9 May, 2008 - 09:37 AM
Post #11


D.I.C Head

**
Joined: 8 Feb, 2008
Posts: 90

QUOTE(Cerolobo @ 8 May, 2008 - 07:41 PM) *

On a side note, I would highly recommend you don't use MS defines for basic types when ever possible. By using them, you are basically forcing anyone who uses your code to use the Microsoft compiler, which is not the best thing to do.
Of course, this isn't true. The typedefs that you list come from a header file that any compiler can compile. Is your anti-Microsoft attitude clouding your technical judgment?

QUOTE(turtleC++ @ 9 May, 2008 - 09:25 AM) *

Sometimes I wonder too about big cap letter and small cap letter of the identifier. People interchange use CHAR then char, and TCHAR. I'm getting confused since I'm new to win32. So from the message above, CHAR is the same as char?

CHAR is the same as char, and WCHAR is the same as wchar_t. TCHAR is switchable; it is the same as CHAR if you don't have UNICODE defined, and is the same as WCHAR if you do have UNICODE defined.

QUOTE(turtleC++ @ 9 May, 2008 - 09:25 AM) *

For DWORD, is it character type or integer type? because I remember I can assign integer to it. Since it has the word WORD at the end of it, so I assume it's character type, right?
Here, a "word" means a "Word of storage". In computers, a "word" is kind of slippery to define since its meaning changes. Neglecting the word's colorful history and meaning, it turns out that a DWORD on Win32 is a 32-bit unsigned integer. It's a numeric type, not a character type.
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

turtleC++
post 9 May, 2008 - 09:46 AM
Post #12


New D.I.C Head

*
Joined: 7 May, 2008
Posts: 13

That's very great explaination, thanks. Now I know CHAR, char, TCHAR and DWORD.

What about bool and BOOL? because when I trace on both of them during the debug mode, they can store 0,1 for true and false, so are they interpreted the same way?

For the TRUE and true, I assume they have the same meaning in term of usage.

By the way, do you have any link or source where can I read more about other different data type because I did found a site from microsoft page, but they didn't give much detail on each data type, so to me they assume programmers should know this.

This post has been edited by turtleC++: 9 May, 2008 - 09:55 AM
User is offlineProfile CardPM
Go to the top of the page
+Quote Post

2 Pages V  1 2 >
Reply to this topicStart new topic
Time is now: 5/16/08 08:47AM

Live C++ Help!

C++ Tutorials

Reference Sheets

C++ Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month