• 【C/C++】字符串编码转换【Windows版】


    StrConv.h

    View Code
    #pragma once
    #include <string>
           
    #ifdef _UNICODE
    
    #ifndef tchar
    #define tchar wchar_t
    #endif
    #ifndef tstring
    #define tstring std::wstring
    #endif
    
    #else
    
    #ifndef tchar
    #define tchar char
    #endif
    #ifndef tstring
    #define tstring std::string
    #endif
    
    #endif    // _UNICODE
    
    //////////////////////////////////////////////////////////////////////////
    // ANSI <-> UTF8 <-> UTF16
    //////////////////////////////////////////////////////////////////////////
    std::wstring AnsiToUtf16(const std::string& str);
    std::string Utf16toUtf8(const std::wstring& str);
    std::string AnsiToUtf8(const std::string& str);
    
    std::string TStrToUtf8(const tstring& str);
    
    std::wstring Utf8ToUtf16(const std::string& str);
    std::string Utf16ToAnsi(const std::wstring& str);
    std::string Utf8ToAnsi(const std::string& str);
    
    tstring Utf8ToTStr(const std::string& str);
    
    #ifdef _UNICODE
    #define TStrToAnsi(str) Utf16ToAnsi(str)
    #define TStrToUtf16(str) (str)
    
    #define AnsiToTStr(str) AnsiToUtf16(str)
    #define Utf16ToTStr(str) (str)
    #else
    #define TStrToAnsi(str) (str)
    #define TStrToUtf16(str) AnsiToUtf16(str)
    
    #define AnsiToTStr(str) (str)
    #define Utf16ToTStr(str) Utf16ToAnsi(str)
    #endif

    StrConv.cpp

    View Code
    #include "StrConv.h"
    #include <windows.h>
    
    std::wstring AnsiToUtf16(const std::string& str)
    {
        std::wstring res;
        int bufferSize = MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.size() + 1, NULL, 0);
        if (bufferSize)
        {
            wchar_t* buffer = new wchar_t[bufferSize];
            if (MultiByteToWideChar(CP_ACP, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize))
                res = buffer;
            delete[] buffer;
            buffer = NULL;
        }
    
        return res;
    }
    
    std::string Utf16toUtf8(const std::wstring& str)
    {
        std::string res;
        int bufferSize = WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, NULL, 0, NULL, NULL);
        if (bufferSize)
        {
            char* buffer = new char[bufferSize];
            if (WideCharToMultiByte(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize, NULL, NULL))
                res = buffer;
            delete[] buffer;
            buffer = NULL;
        }
    
        return res;
    }
    
    std::string AnsiToUtf8(const std::string& str)
    {
        return Utf16toUtf8(AnsiToUtf16(str));
    }
    
    std::string TStrToUtf8(const tstring& str)
    {
    #ifdef _UNICODE
        return Utf16toUtf8(str);
    #else
        return AnsiToUtf8(str);
    #endif
    }
    
    std::wstring Utf8ToUtf16(const std::string& str)
    {
        std::wstring res;
        int bufferSize = MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, NULL, 0);
        if (bufferSize)
        {
            wchar_t* buffer = new wchar_t[bufferSize];
            if (MultiByteToWideChar(CP_UTF8, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize))
                res = buffer;
            delete[] buffer;
            buffer = NULL;
        }
    
        return res;
    }
    
    std::string Utf16ToAnsi(const std::wstring& str)
    {
        std::string res;
        int bufferSize = WideCharToMultiByte(CP_ACP, 0, str.c_str(), (int)str.size() + 1, NULL, 0, NULL, NULL);
        if (bufferSize)
        {
            char* buffer = new char[bufferSize];
            if (WideCharToMultiByte(CP_ACP, 0, str.c_str(), (int)str.size() + 1, buffer, bufferSize, NULL, NULL))
                res = buffer;
            delete[] buffer;
            buffer = NULL;
        }
    
        return res;
    }
    
    std::string Utf8ToAnsi(const std::string& str)
    {
        return Utf16ToAnsi(Utf8ToUtf16(str));
    }
    
    tstring Utf8ToTStr(const std::string& str)
    {
    #ifdef _UNICODE
        return Utf8ToUtf16(str);
    #else
        return Utf8ToAnsi(str);
    #endif
    }
  • 相关阅读:
    腾讯TBS加载网页无法自适应记录
    filter过滤器实现验证跳转_返回验证结果
    Oracle不连续的值,如何实现查找上一条、下一条
    springmvc.xml 中 <url-pattern></url-pattern>节点详解
    spring拦截器-过滤器的区别
    (转)spring中的拦截器(HandlerInterceptor+MethodInterceptor)
    @Value("${xxxx}")注解的配置及使用
    mybatis BindingException: Invalid bound statement (not found)
    spring声明式事务管理方式( 基于tx和aop名字空间的xml配置+@Transactional注解)
    Spring事务管理详解_基本原理_事务管理方式
  • 原文地址:https://www.cnblogs.com/jeJee/p/2675072.html
Copyright © 2020-2023  润新知