Unicode下 LPCTSTR 转 string

来源:互联网 发布:邮箱注册淘宝个人账号 编辑:程序博客网 时间:2024/05/17 23:18

最近因工作需要,将原有多字节项目转到Unicode编码。状况百出,特此记录。

LPCTSTR uIp = _T("127.0.0.1");

string ip = LPSTR(uIp);

输出是多少?是 1,没错,就是1尴尬

怎么解决,用此宏->CW2A 请看下文测试过程。


// UnicodeTest.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <string>
#include <iostream>
#include <afx.h>
using namespace std;


void F(LPCTSTR rhs)
{
    string ip = (LPSTR)rhs;
    cout << ip <<endl;

}

int _tmain(int argc, _TCHAR* argv[])
{

    LPCTSTR uIp = _T("127.0.0.1");
    string ip = LPSTR(uIp);
    cout <<ip.data() <<endl;

    F(uIp);

    ip = CW2A(uIp);
    cout << ip.data() <<endl;


    return 0;
}



可以看到,经过CW2A,最终输出127.0.0.1.而直接用LPSTR转输出为1