Wide chars in C++

Thameera Senanayaka

Allion Technologies

#include <iostream>

int main() {
    const char* str = "Hello";

    std::cout << str << std::endl;
    std::cout << strlen(str) << std::endl;
}
Hello
5

ASCII characters

#include <iostream>

int main() {
    const char* str = "こんにちは";

    std::cout << str << std::endl;
    std::cout << strlen(str) << std::endl;
}
こんにちは
10

Non-ASCII characters

wchar_t

  • Wide character type
  • A keyword (not a class)
  • Not directly tied to unicode
#include <iostream>

int main() {
    const wchar_t* str = L"こんにちは";

    std::wcout << str << std::endl;
    std::wcout << wcslen(str) << std::endl;
}

.

wchar_t usage

#include <iostream>
#include <fcntl.h>
#include <io.h>

int main() {
    _setmode(_fileno(stdout), _O_WTEXT);
    const wchar_t* str = L"こんにちは";

    std::wcout << str << std::endl;
    std::wcout << wcslen(str) << std::endl;
}
こんにちは

5

Printing correctly on console

#include <iostream>

int main() {
    std::cout << sizeof(wchar_t) << std::endl;
}
2

wchar_t size

#include <iostream>
#include <fcntl.h>
#include <io.h>

int main() {
    _setmode(_fileno(stdout), _O_WTEXT);
    const wchar_t* str = L"こんにちは";

    std::wcout << int(&str[0]) << std::endl;
    std::wcout << int(&str[1]) << std::endl;
    std::wcout << int(&str[2]) << std::endl;
}
10115120
10115122
10115124

wchar_t size

#include <iostream>
#include <fcntl.h>
#include <io.h>

int main() {
    _setmode(_fileno(stdout), _O_WTEXT);
    const wchar_t* str = L"hello";

    std::wcout << int(&str[0]) << std::endl;
    std::wcout << int(&str[1]) << std::endl;
    std::wcout << int(&str[2]) << std::endl;
}
17061936
17061938
17061940

wchar_t size

#include <iostream>
#include <fcntl.h>
#include <io.h>

int main() {
    _setmode(_fileno(stdout), _O_WTEXT);
    const char* str = "hello";

    const size_t size = strlen(str) + 1;
    wchar_t* wstr = new wchar_t[size];
    mbstowcs(wstr, str, size);

    std::wcout << wstr << std::endl;
}
hello

char to wchar_t

#include <iostream>
#include <fcntl.h>
#include <io.h>

int main() {
    _setmode(_fileno(stdout), _O_WTEXT);
    const wchar_t* wstr = L"hello";
    const size_t size = wcslen(wstr)+1;
    char* str = new char[size];
    wcstombs(str, wstr, size);

    std::wcout << str << std::endl;
}
hello

wchar_t to char

std::wstring

  • A class from STL
  • Has same methods as std::string
  • Uses wchar_t (like std::string uses char)
#include <iostream>
#include <fcntl.h>
#include <io.h>
#include <string>


int main() {
    _setmode(_fileno(stdout), _O_WTEXT);

    std::wstring wstr = L"こんにちは";

    std::wcout << wstr << std::endl;
}
こんにちは

std::wstring

Wide chars in Windows

LPCSTR str = "Hello";

typedef __nullterminated CONST CHAR *LPCSTR, *PCSTR;
LPCWSTR wstr = L"Hello";

typedef wchar_t WCHAR; 
typedef __nullterminated CONST WCHAR *LPCWSTR, *PCWSTR;

Wide chars in Windows

MessageBoxW(NULL, L"Test", L"Title", MB_OK);

int
WINAPI
MessageBoxW(
    __in_opt HWND hWnd,
    __in_opt LPCWSTR lpText,
    __in_opt LPCWSTR lpCaption,
    __in UINT uType);


#ifdef UNICODE
#define MessageBox  MessageBoxW
#else
#define MessageBox  MessageBoxA
#endif // !UNICODE

Thank you!

Made with Slides.com