H
H
Hakito2016-03-02 14:24:35
C++ / C#
Hakito, 2016-03-02 14:24:35

How to call MultiByteToWideChar?

Hello!
I need to convert text from ASCII to Unicode
To do this, I write:

setlocale(LC_ALL, "Russian");
        char* c="фывапр";
  int q = CP_ACP;
  int b = MultiByteToWideChar(q, 0, c, -1, 0, 0);
  wchar_t* o = new wchar_t[b];
  MultiByteToWideChar(q, 0, c, -1, o, b);	
  
  printf("%s\n", o);

Outputs: D♦K♦2♦0♦?♦@♦
The project settings have a multi-byte encoding (as I understand it, it is one-byte)
What needs to be changed so that it translates normally?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
V
Vladimir Martyanov, 2016-03-02
@hakito

So it is probably normal and translates. Only printf does not know how to work with this. Try wprintf instead.

R
res2001, 2016-03-02
@res2001

Declare Unicode constants like this:
And output to the console using wprintf, as Vladimir Martyanov wrote

A
Alexander Titov, 2016-03-02
@alex-t

The conversion is most likely correct, if you want to check the result without problems, look in the VisualStudio debugger, but if there are problems, then master the Windows console. Only this perverted way works there:

#include <windows.h>
....
DWORD n;
WriteConsoleW(GetStdHandle(STD_OUTPUT_HANDLE), L"фыва", 4, &n, 0);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question