rdu-cpp

杨柳不折
随笔 - 13, 文章 - 0, 评论 - 4, 引用 - 0
数据加载中……

Translating Strings Between Unicode and ANSI

// 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;
}

posted on 2009-04-22 17:00 rdu 阅读(450) 评论(0)  编辑 收藏 引用


只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理