Go Premium for a chance to win a PS4. Enter to Win

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 796
  • Last Modified:

UTF-8 Encoding Need to convert CHAR to WCHAR?

Hi all,

I want to work My application in Non-english Os.

As of now , my application follow ISO-8859-1 encoding.For I18N, i m going to change UTF-8.

My dought is , part of my application is c/c++.

Shall i need to convert char (char* and LPSTR)  to wchar (wchar * and LPWSTR) or No need to convert  my appl ?

(why i m asking is,  i wrote small stand alone program to read a non english (Japanese) letter from registry Using LPSTR/CHAR.Its Working)

Any help will be appreciated?
2 Solutions
>>>> i m going to change UTF-8.

UTF-8 is a 8bit multibyte format for UNICODE which wasn't well supported by C++. Nevertheless you could store the text and strings in normal char arrays (not wchar_t) but it is badly readable if actually you have non-ASCII letters to print.

When using wchar_t (with MS VC compiler) you have 16bit chars with a character set that MS calls UNICODE. Then, indeed you have to turn from char to wchar_t and LPCSTR to LPWCSTR and so on and each literal you have to prefix with a L, e. g. L"Hello World". Or, you use the so-called T-switch with TCHAR and LPCTSTR, ... and literals as _T("Hello World). For the latter you would need to set your project to UNICODE. I personally do not recommend to using the T-switch cause it doesn't allow a proper use of both ANSI strings and wide strings and generates a lot of problems you wouldn't have without T-switch.

when you want to change your all chars to w_char, you should:
check your all string functions like strcpy, strcat, strstr and etc and then change them to wcscpy, wcscat, wcsstr and etc.

then check your API functions:
in default, when you use for example CreateFile() API function, you call CreateFileA() that suitable for english filenames but you should change all of API function calls that has A/W to W
I mean if you use CreateFile change this to CreateFileW
or you can add #define UNICODE top of #include <windows.h> like this:
#define UNICODE
#include <windows.h>
or if you are using VS, you can go Project -> Settings -> C/C++ tab
in the C/C++ tab, you'll see preprocessor definitions like this:
you can change _MBCS to _UNICODE. then it should be:
then, all of your API calls will be W as default (I mean as non-english strings)


Featured Post

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now