zoukankan      html  css  js  c++  java
  • 【转】 Uniode TO ANSI 转换函数封装

    ===================================================================

    // UNICODE 转换 ANSI 程序
    int ustr_astr( CString unicodestr, char *ansistr )
    {
    int result = 0;
    try
    {
       int needlen = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, NULL, 0, NULL, NULL );
       if( needlen < 0 )
       {
        return needlen;
       }
       result = WideCharToMultiByte( CP_ACP, 0, unicodestr, -1, ansistr, needlen + 1, NULL, NULL );
       if( result < 0 )
       {
        return result;
       }
       return strlen( ansistr );
    }
    catch( ... )
    {
    }
    return result;
    }

    使用方式

    ustr_astr(csMsg,usef);

    --------------------------------------------------------------------------------------------------------
    // ANSI 转换 UNICODE 程序
    int astr_ustr(char *ansistr ,WCHAR *unicodestr)
    {
    int result = 0;
    try
    {
       int needlen = MultiByteToWideChar( CP_ACP, 0, ansistr, -1, NULL, 0);
       if( needlen < 0 )
       {
        return needlen;
       }
       result = MultiByteToWideChar( CP_ACP, 0, ansistr, -1, unicodestr, needlen + 1);
       if( result < 0 )
       {
        return result;
       }
       return strlen(ansistr);
    }
    catch( ... )
    {
    }
    return result;
    }

    使用方式

    astr_ustr(csMsg,usef);

    =================================================================

    注意 以下 注释的 如果事先用 new char[XX];分配过内存的话 就不需要在使用了。

    BOOL AnstrToUnstr (PSTR pMultiByteStr, PWSTR pWideCharStr) ;
    BOOL AnstrToUnstr (PSTR pMultiByteStr, PWSTR pWideCharStr) {

       int nLenOfWideCharStr;   
       BOOL fOk = FALSE;   
       
       nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, -1, NULL, 0);   
    // pWideCharStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, nLenOfWideCharStr * sizeof(wchar_t));   
    //if (pWideCharStr == NULL)   
    //    return(fOk);   
       MultiByteToWideChar(CP_ACP, 0, pMultiByteStr, -1, pWideCharStr, nLenOfWideCharStr);   
       
    // HeapFree(GetProcessHeap(), 0, pWideCharStr);   
       return(fOk);   
    }

    ---------------------------------------------------------------------------------------------------

    BOOL UnstrToAnstr (PWSTR pWideCharStr, PSTR pMultiByteStr) ;
    BOOL UnstrToAnstr (PWSTR pWideCharStr, PSTR pMultiByteStr) {

       int nLenOfMultiByteStr; 
       BOOL fOk = FALSE;   

       nLenOfMultiByteStr = WideCharToMultiByte(CP_ACP, 0, pWideCharStr, -1,pMultiByteStr, (int)strlen(pMultiByteStr), NULL, NULL);   
    // pMultiByteStr = (PSTR)HeapAlloc(GetProcessHeap(), 0, nLenOfMultiByteStr * sizeof(char));
    //    if (pMultiByteStr == NULL)   
    //    return(fOk); 
        WideCharToMultiByte(CP_ACP, 0, pWideCharStr, -1,pMultiByteStr,nLenOfMultiByteStr, NULL, NULL);   
       
    // HeapFree(GetProcessHeap(), 0, pMultiByteStr);   
       return(fOk);   
    }

  • 相关阅读:
    CSS text-indent 属性
    从零开始搭建一个简单的基于webpack的vue开发环境
    mui is not defined
    vue项目中引入mui.poppicker.js文件时报错“Uncaught ReferenceError: mui is not defined”
    vue-cli创建的项目中引入第三方库报错'caller', 'calle', and 'arguments' properties may not be...
    微信小程序点击顶部导航栏切换样式
    js学习笔记-日期对象
    setTimeout()和setInterval()的用法及区别
    Ubuntu 14.04 下安装redis后运行redis-cli 报出redis Connection refused错误【已解决】
    Ubuntu start:未知任务:mysql
  • 原文地址:https://www.cnblogs.com/Daywei/p/1940226.html
Copyright © 2011-2022 走看看