• 多字节


    void ConvertGBKToUtf8(CString& strGBK)
    {
    int len=MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, NULL,0);
    unsigned short * wszUtf8 = new unsigned short[len+1];
    memset(wszUtf8, 0, len * 2 + 2);
    MultiByteToWideChar(CP_ACP, 0, (LPCTSTR)strGBK, -1, wszUtf8, len);

         len = WideCharToMultiByte(CP_UTF8, 0, wszUtf8, -1, NULL, 0, NULL, NULL);
         char *szUtf8=new char[len + 1];
         memset(szUtf8, 0, len + 1);        
         WideCharToMultiByte (CP_UTF8, 0, wszUtf8, -1, szUtf8, len, NULL,NULL);
    
         strGBK = szUtf8;
         delete[] szUtf8;                
         delete[] wszUtf8;                
    

    }

    void ConvertUtf8ToGBK(CString& strUtf8)
    {
    int len=MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, NULL,0);
    unsigned short * wszGBK = new unsigned short[len+1];
    memset(wszGBK, 0, len * 2 + 2);
    MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUtf8, -1, wszGBK, len);

         len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);
         char *szGBK=new char[len + 1];
         memset(szGBK, 0, len + 1);
         WideCharToMultiByte (CP_ACP, 0, wszGBK, -1, szGBK, len, NULL,NULL);
    
         strUtf8 = szGBK;
         delete[] szGBK;
         delete[] wszGBK;
    

    }

    qp::StringW Global::AnsiToUnicode(const char* buf)
    {
    int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
    if (len == 0) return L"";

    std::vector<wchar_t> unicode(len);  
    ::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);  
    
    return &unicode[0];  
    

    }

    qp::StringA Global::UnicodeToAnsi(const wchar_t* buf)
    {
    int len = ::WideCharToMultiByte(CP_ACP, 0, buf, -1, NULL, 0, NULL, NULL);
    if (len == 0) return "";

    std::vector<char> utf8(len);  
    ::WideCharToMultiByte(CP_ACP, 0, buf, -1, &utf8[0], len, NULL, NULL);  
    
    return &utf8[0];  
    

    }

    qp::StringW Global::Utf8ToUnicode(const char* buf)
    {
    int len = ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, NULL, 0);
    if (len == 0) return L"";

    std::vector<wchar_t> unicode(len);  
    ::MultiByteToWideChar(CP_UTF8, 0, buf, -1, &unicode[0], len);  
    
    return &unicode[0];  
    

    }

    qp::StringA Global::UnicodeToUtf8(const wchar_t* buf)
    {
    int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
    if (len == 0) return "";

    std::vector<char> utf8(len);  
    ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);  
    
    return &utf8[0];  
    

    }

  • 相关阅读:
    Serverless 解惑——函数计算如何访问 MySQL 数据库
    Kubernetes 会不会“杀死” DevOps?
    开发函数计算的正确姿势——使用交互模式安装依赖
    从零开始入门 K8s | 调度器的调度流程和算法介绍
    eclipse中如何自动生成构造函数
    微服务架构中API网关的角色
    JAVA设计模式之责任链模式
    谦先生的程序员日志之我的hadoop大数据生涯一
    谦先生的bug日志之hive启动权限问题
    CSS盒子模型之详解
  • 原文地址:https://www.cnblogs.com/hllak/p/4368763.html
Copyright © 2020-2023  润新知