zoukankan      html  css  js  c++  java
  • wchar_t与char转换(总结)

    #include<string>//标准C++;

    string   tp;
    wchar_t   *s;
    char   *d;
    tp=s;
    d=tp.c_str();
    也可使用WideCharToString(wchar_t   *   Source);函数转换

    WideCharToMultiByte

    MultiByteToWideChar

    LPSTR = char *
    LPCSTR = const char *
    LPWSTR = wchar_t *
    LPCWSTR = const wchar_t *
    LPOLESTR = OLECHAR * = BSTR = LPWSTR(Win32)
    LPCOLESTR = const OLECHAR * = LPCWSTR(Win32)
    LPTSTR = _TCHAR *
    LPCTSTR = const _TCHAR *

    #include <windows.h>
    #include <stdio.h>
    
    //function: charTowchar
    //purpose:char to WCHAR 、wchar_t、LPWSTR etc
    void charTowchar(const char *chr, wchar_t *wchar, int size)
    {   
    	MultiByteToWideChar( CP_ACP, 0, chr,
            strlen(chr)+1, wchar, size/sizeof(wchar[0]) );
    }
    
    //function: wcharTochar
    //purpose:WCHAR 、wchar_t、LPWSTR to char
    void wcharTochar(const wchar_t *wchar, char *chr, int length)
    {
        WideCharToMultiByte( CP_ACP, 0, wchar, -1,
            chr, length, NULL, NULL );
    }
    
    int main (void)
    {
    	char     chr[128];
    	wchar_t  *wchar = L"陈鸿钦";
    	
    
    	//wchar_t to char
    	wcharTochar(wchar, chr, sizeof(chr));
    	printf("char is %s\n", chr);
    
    	//char to wchar_t
    	wchar = (wchar_t *)malloc(sizeof(wchar_t) * 64);
    	charTowchar(chr, wchar, sizeof(wchar_t) * 64);
    	
    	wprintf_s(L"%s\n", wchar);//
    	getchar();
    
    	return 0;
    }

    Requirements:  Ws2_32.lib

    函数原型:
    int WideCharToMultiByte(
      UINT CodePage,
      DWORD dwFlags,
      LPCWSTR lpWideCharStr,
      int cchWideChar,
      LPSTR lpMultiByteStr,
      int cbMultiByte,
      LPCSTR lpDefaultChar,   
      LPBOOL lpUsedDefaultChar
    );

    参数说明:
    1、CodePage
    指定要转换成的字符集代码页,它可以是任何已经安装的或系统自带的字符集,可以选择以下的代码页:
    CP_ACP         //当前系统ANSI代码页
    CP_MACCP       //当前系统Macintosh代码页
    CP_OEMCP       //当前系统OEM代码页,一种原始设备制造商硬件扫描码
    CP_SYMBOL      //Symbol代码页,用于Windows 2000及以后版本
    CP_THREAD_ACP  //当前线程ANSI代码页,用于Windows 2000及以后版本
    CP_UTF7  //UTF-7,设置此值时lpDefaultChar和lpUsedDefaultChar都必须为NULL
    CP_UTF8  //UTF-8,设置此值时lpDefaultChar和lpUsedDefaultChar都必须为NULL

    2、dwFlags
    指定如何处理没有转换的字符,一般情况下设为0
    WC_NO_BEST_FIT_CHARS  //把不能直接转换成相应多字节字符的Unicode字符转换成lpDefaultChar指定的默认字符。
    WC_COMPOSITECHECK     //把合成字符转换成预制的字符。它可以与后三个选项中的任何一个组合使用,如果没有与他们中的任何一个组合,则与选项WC_SEPCHARS相同。
    WC_ERR_INVALID_CHARS  //此选项会致使函数遇到无效字符时失败返回,并且GetLastError会返回错误码ERROR_NO_UNICODE_TRANSLATION。否则函数会自动丢弃非法字符。此选项只能用于UTF8
    WC_DISCARDNS      //转换时丢弃不占空间的字符,与WC_COMPOSITECHECK一起使用
    WC_SEPCHARS       //转换时产生单独的字符,此是默认转换选项,与WC_COMPOSITECHECK一起使用
    WC_DEFAULTCHAR    //转换时使用默认字符代替例外的字符,(最常见的如"?"),与WC_COMPOSITECHECK一起使用

    3、lpWideCharStr  //要转换的宽字符串
    4、cchWideChar    //要转换宽字符串的长度,-1表示转换到字符串结尾
    5、lpMultiByteStr //接收转换后输出的字符串的缓冲区
    6、cbMultiByte    //输出缓冲区大小,如果为0,lpMultiByteStr将被忽略,函数将返回所需缓冲区大小而不使用lpMultiByteStr。
    7、lpDefaultChar  //指向字符的指针,在指定编码里找不到相应字符时使用此字符作为默认字符代替,如果为NULL则使用系统默认字符
    8、lpUsedDefaultChar  //开关变量的指针,用以表明是否使用过默认字符,可设为NULL

    返回值:为0表示调用失败

    下面就来看看如何将wstring和wchar_t*转换为string和char*
    wstring转换为string:

    wstring wstr=_T("翔翔糖糖");
    int size=WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,NULL,0,NULL,NULL);
    char *ch=new char[size+1];
    if(!WideCharToMultiByte(CP_ACP,0,wstr.c_str(),-1,ch,size,NULL,NULL))
    {
        return false;
    }
    string str=ch;

    wchar_t*转换为char*:
    wchar_t *wch=_T("翔翔糖糖");
    int size=WideCharToMultiByte(CP_ACP,0,wch,-1,NULL,0,NULL,NULL);
    char *ch=new char[size+1];
    if(!WideCharToMultiByte(CP_ACP,0,wch,-1,ch,size,NULL,NULL))
    {
        return false;
    }

    wchar_t* wstr = L"A wide character string.";

    char* ascii = new char[wcslen(wstr) + 1];

    wcstombs( ascii, wstr, wcslen(wstr) );

    另一种方法:                                                                                                                          

    char* localeInfo;                                                                                                                

    wchar_t *pwchello = L"/x3042/x3043"; // 2 Hiragana characters                                      

    wchar_t *pwc;

    wchar_t*转换为char*:                                                                                                       

    size_t size;                                                                                                                           

     int requiredSize;                                                                                                                  

    requiredSize = wcstombs( NULL, pwchello, 0);                                                                   

    pmbhello = (unsigned char *)malloc( requiredSize + 1);                                              

    size = wcstombs( pmbhello, pwchello, requiredSize + 1);

    char* 转换为 wchar_t*                                                                                                      

    size_t size;                                                                                                                           

     int requiredSize;                                                                                                                 

     requiredSize = mbstowcs(NULL, pmbhello, 0);                                                                    

    pwc = (wchar_t *)malloc( (requiredSize + 1) * sizeof( wchar_t ));                                      

    size = mbstowcs( pwc, pmbhello, requiredSize + 1);

    请问怎么样把string转换到LPCTSTR

    std::string   str;
    LPCTSTR   lpstr   =   (LPCTSTR)str.c_str();

    MFC中Messagebox如何输出数字

  • 相关阅读:
    2019.8.6原型链与继承
    2019.8.2闭包,作用域
    2019.8.1正则二
    2019.7.31正则
    2019.7.29二维数组
    2019.7.28关于数组和循环的八道题
    2019.7.27数组api
    DOM
    JavaScript数组5种去重方法
    JavaScript面向对象
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5834990.html
Copyright © 2011-2022 走看看