What is the simplest way to convert char[] to/from tchar[] in C/C++(ms)?
Asked Answered
P

8

18

This seems like a pretty softball question, but I always have a hard time looking up this function because there seem there are so many variations regarding the referencing of char and tchar.

Postgraduate answered 1/10, 2008 at 20:0 Comment(0)
I
7

MultiByteToWideChar but also see "A few of the gotchas of MultiByteToWideChar".

Irrationality answered 1/10, 2008 at 20:5 Comment(2)
What happens if TCHAR is CHAR?Langobardic
I read the question as stipulating that a conversion was necessary, but point taken.Irrationality
C
10

The simplest way is to use the conversion macros:

  • CW2A
  • CA2W
  • etc...

MSDN

Complementary answered 1/10, 2008 at 20:22 Comment(2)
we should not use these conversions, because if we call these in recursion, it won't release the memoryTestimonial
@Testimonial 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.Emu
D
8

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()

Dulles answered 1/10, 2008 at 20:3 Comment(1)
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.Postgraduate
I
7

MultiByteToWideChar but also see "A few of the gotchas of MultiByteToWideChar".

Irrationality answered 1/10, 2008 at 20:5 Comment(2)
What happens if TCHAR is CHAR?Langobardic
I read the question as stipulating that a conversion was necessary, but point taken.Irrationality
P
3

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

UTF8 to/from wide char conversion in STL

Prate answered 1/10, 2008 at 20:9 Comment(1)
How can it be crosss platform there is no TCHAR anyware else. Its windows specific.Langobardic
C
3

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

Cleodal answered 1/10, 2008 at 20:25 Comment(0)
M
2

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

http://www.wincli.com/?p=72

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

Mckeever answered 27/10, 2010 at 3:32 Comment(1)
broken link (thats why is better to quote the sample : )Draghound
H
1

You can put condition in your code

ifdef _UNICODE

{ //DO LIKE TCHAR IS WIDE CHAR } ELSE { //DO LIKE TCHAR IS CHAR }

Hartz answered 31/5, 2016 at 8:28 Comment(0)
J
0

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

The way this appears to be done now is to use the TEXT macro. The example for FindFirstFile at msdn points this out. http://msdn.microsoft.com/en-us/library/windows/desktop/aa364418%28v=vs.85%29.aspx

Jameljamerson answered 26/1, 2012 at 17:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.