|
// DoUKnow.cpp : main project file.
#include "stdafx.h" #include <iostream> #include <windows.h> #include <tchar.h> using namespace std;
int main(){ // the ANSI string to convert PCSTR pCStr = "this is an ANSI string";
// Step 1: Calculate required size int i = MultiByteToWideChar(CP_ACP, NULL, pCStr, -1, NULL, 0); int size = i * sizeof(WCHAR);
// Step 2: Malloc buffer WCHAR *szBuffer = (WCHAR*)malloc(size);
// Step 3: Do the conversion MultiByteToWideChar(CP_ACP, NULL, pCStr, -1, szBuffer, size); // Use the converted Unicode string wcout << szBuffer << endl;
// Step 4: Free the buffer delete szBuffer; system("pause"); return 0; }
// DoUKnow.cpp : main project file.
#include "stdafx.h" #include <iostream> #include <windows.h> #include <tchar.h> using namespace std;
int main(){ // the ANSI string to convert PCWSTR pWStr = L"this is an Unicode string";
// Step 1: Calculate required size int i = WideCharToMultiByte(CP_ACP, NULL, pWStr, -1, NULL, 0, NULL, NULL); int size = i * sizeof(CHAR);
// Step 2: Malloc buffer CHAR *szBuffer = (CHAR*)malloc(size);
// Step 3: Do the conversion WideCharToMultiByte(CP_ACP, NULL, pWStr, -1, szBuffer, size, NULL, NULL); // Use the converted Unicode string cout << szBuffer << endl;
// Step 4: Free the buffer delete szBuffer; system("pause"); return 0; }
|