TCHAR Conversions from char*
-
I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!
-
I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!
-
I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!
Not Knuth wrote:
How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else?
wcstombs
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow
cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You
-
I need some help converting between TCHAR strings and ASCII strings. (I understand that the actual type for TCHAR is decided by the compiler switches.) In this case, TCHAR is UNICODE. But I have a char* string passed into my function that I need to deal with. My questions: What's the best way to convert the char* string (array) to TCHAR? I'm doing it one char at a time in a loop, but I am hoping for something more elegant. How do I handle the ASCII end of string in TCHAR? For example, in one section I need to loop through the characters in the TCHAR string. I want to stop if I hit a new line from the char* string. That's easy in an ASCII string, but in UNICODE... well it's probably just as easy if you know what to look for! :~ Should I be looking for TWO TCHAR characters (0xA and 0x0)? or something else? Thank you!
Place the macro USES_CONVERSION at the beginning of your function. You then have access to all of the string conversion macros. You can convert between any of ACSII (A), OLE (OLE), WIDE (W)and TCHAR (T). To convert ASCII to TCHAR do the following. LPSTR *lpSrc; LPTSTR *lpDest = A2T(lpSrc); Similarly T2A, A2W, W2A, A2OLE, etc can be used. You can also put a C after the 2 to get a const string returned. The good thing about these macros is that if you build the application with _MBCS defined, TCHAR is the same as ASCII so the A2T macros becomes nothing. If you build with _UNICODE defined then it will allocate a buffer and covert to Unicode for you. The memory is allocated on the stack with _alloca, so you can't keep the converted string after the function returns without taking a proper copy of it first. Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.
-
Place the macro USES_CONVERSION at the beginning of your function. You then have access to all of the string conversion macros. You can convert between any of ACSII (A), OLE (OLE), WIDE (W)and TCHAR (T). To convert ASCII to TCHAR do the following. LPSTR *lpSrc; LPTSTR *lpDest = A2T(lpSrc); Similarly T2A, A2W, W2A, A2OLE, etc can be used. You can also put a C after the 2 to get a const string returned. The good thing about these macros is that if you build the application with _MBCS defined, TCHAR is the same as ASCII so the A2T macros becomes nothing. If you build with _UNICODE defined then it will allocate a buffer and covert to Unicode for you. The memory is allocated on the stack with _alloca, so you can't keep the converted string after the function returns without taking a proper copy of it first. Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.
John Saunders wrote:
Place the macro USES_CONVERSION at the beginning of your function
John Saunders wrote:
Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.
If you use the newer ATL7 conversion macros then you do not have to use USES_CONVERSION and you can safely use the conversion macros inside a loop. http://msdn2.microsoft.com/en-us/library/87zae4a3.aspx[^]
You may be right
I may be crazy
-- Billy Joel --Within you lies the power for good, use it!!!
-
Place the macro USES_CONVERSION at the beginning of your function. You then have access to all of the string conversion macros. You can convert between any of ACSII (A), OLE (OLE), WIDE (W)and TCHAR (T). To convert ASCII to TCHAR do the following. LPSTR *lpSrc; LPTSTR *lpDest = A2T(lpSrc); Similarly T2A, A2W, W2A, A2OLE, etc can be used. You can also put a C after the 2 to get a const string returned. The good thing about these macros is that if you build the application with _MBCS defined, TCHAR is the same as ASCII so the A2T macros becomes nothing. If you build with _UNICODE defined then it will allocate a buffer and covert to Unicode for you. The memory is allocated on the stack with _alloca, so you can't keep the converted string after the function returns without taking a proper copy of it first. Also don't use inside a loop because it could allocate a lot of memory if the loop iteration is large.
Thanks, this works fine. But I am wondering how the macros work. I followed the code and see that it ultimately ends up at defs for MultiByteToWideChar and others. But of course the code is not shown. So I wonder if this is implemented as just a big lookup table or if there is some better way to approach this. Obviously this question has become somewhat academic at this point. My initial problem is resolved. But I am just thinking about how I would have implemented this and I don't see a better way than using a big lookup. That seems too clumsy.
-
Thanks, this works fine. But I am wondering how the macros work. I followed the code and see that it ultimately ends up at defs for MultiByteToWideChar and others. But of course the code is not shown. So I wonder if this is implemented as just a big lookup table or if there is some better way to approach this. Obviously this question has become somewhat academic at this point. My initial problem is resolved. But I am just thinking about how I would have implemented this and I don't see a better way than using a big lookup. That seems too clumsy.
An alternative to using the ATL macros is to use the
_bstr_t
class. It has constructors that take both ASCII and WIDE character sets, and have overloaded operators for bothchar*
andwchar_t*
. It will end up calling the same functions in the end (the MultiByteToWide, etc.) but it easier to read and debug since it isn't a macro.If you decide to become a software engineer, you are signing up to have a 1/2" piece of silicon tell you exactly how stupid you really are for 8 hours a day, 5 days a week Zac