I'd like to convert my visual studio project from MBCS to Unicode CS. But I have like 900 const char arrays in my source code, is there an automatic way to do it? it is very a lot to do line by line. In doing I'm referring to adding _T("asd") to "asd".
-
As long as you have one string per line you could use a regular expression for search and replace. When you have more than one string per line, you will have to click "no" at each invalid search result. Try search regular expression: {[^L]}{:q} Replace with: \1L"\2" – harper Sep 05 '12 at 14:12
-
You can use regular expressions in searching in VS???? WOW. – AlexandruC Sep 05 '12 at 14:20
2 Answers
There's no way around this.
A string literal is interpreted/encoded as "char *" unless you use L
to tell the compiler to always encode it as Unicode (WCHAR/wide string), or you use the _T
macros to have it encoded depending on whether the "Character Set" is set to ANSI/MBCS or Unicode..
You might be tempted to change the encoding of your source files with "File | Advanced Save Options" to Unicode...thinking that this will change how your string literals are encoded...but no...they will still be treated as narrow strings.
Some links that may help understand the options.
Visual C++: Migrating traditional C and C++ string code to a Unicode world
http://www.codeproject.com/Articles/2995/The-Complete-Guide-to-C-Strings-Part-I-Win32-Chara
Your best bet is to write a "macro" in Visual Studio that you can run and it will go through the set of source code files containing your string literals and adds the _T( )
or L
prefix to those strings.
How sophisticated you make the macro is up to you...but it should be possible to get the macro to pattern match a string, and then asks you for confirmation as to whether to prefix/wrap the string literal (with L or _T())....because you don't want to convert all the strings in your source...because some will need to stay as narrow strings.
Another option is you could use the Find dialog to find strings, and then visually see if they need modifying and manually do the modification.
And finally, you might be able to leverage a "translation tool" that is following the "in-place localized replacement of strings in source files and then recompile" localization technique (as opposed to the "extract strings out to Resources" technique...which is the more common one). I know there are tools like that, that can extract strings from source files...build a table/database of those strings...which then get translated, and then re-inserted....(in fact I wrote something to do that in the past). Maybe you could find a tool that does that...but modify it slightly to insert the L prefix when inserting the strings back in.
In fact, if your taking the trouble to make your application Unicode...then I would imagine at some point you will be intending to support different localizations of your application....so what you could do is move all your strings to Resources...and Resources hold them as Unicode...you'd have to modify you code to get the strings loaded from resources, etc.

- 1
- 1

- 12,375
- 4
- 39
- 47
I made a regex substitution which I used in VIM to do this in a large project which had a mixture of "abc", _T("abc"), and other strings:
:%s/\([^T][^(L]\)"\([^"()]*\)"/\1_T("\2")/g
Then I used version control (TortoiseSVN-diff) to review the changes, reverting them as necessary. YMMV, but this was the fastest option for my project.

- 69
- 1
- 3