字符串编码转换(Win32, C++)

文章介绍了CStrConvert类,它提供了一系列静态方法,用于在Unicode、ANSI字符和UTF-8编码之间进行宽字符串和多字节字符之间的转换,使用了WindowsAPI函数如WideCharToMultiByte和MultiByteToWideChar。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

CStrConvert.h

#pragma once

#include <wtypesbase.h>
#include <string>
#ifdef _UNICODE
using _tstring = std::wstring;
#else
using _tstring = std::string;
#endif

class CStrConvert
{
public:

    // 宽字符串转换
    static std::string WStrToU8Str(const std::wstring& str);
    static std::string WStrToAStr(const std::wstring& str);
    static _tstring WStrToTStr(const std::wstring& str);

    // ANSI字符转换
    static std::wstring AStrToWStr(const std::string& str);
    static std::string AStrToU8Str(const std::string& str);
    static _tstring AStrToTStr(const std::string& str);

    // UTF-8字符串转换
    static std::wstring U8StrToWStr(const std::string& str);
    static std::string U8StrToAStr(const std::string& str);
    static _tstring U8StrToTStr(const std::string& str);

    // 中立字符串转换
    static std::string TStrToAStr(const _tstring& str);
    static std::wstring TStrToWStr(const _tstring& str);
    static std::string TStrToU8Str(const _tstring& str);

private:

    // 宽字符转多字节字符
    static std::string _WStrToMultiStr(UINT CodePage, const std::wstring& str);

    // 多字节字符转宽字符
    static std::wstring _MultiStrToWStr(UINT CodePage, const std::string& str);
};

CStrConvert.cpp

#include "CStrConvert.h"

std::string CStrConvert::_WStrToMultiStr(UINT CodePage, const std::wstring& str)
{
    //计算缓冲区所需的字节长度
    int cbMultiByte = ::WideCharToMultiByte(CodePage, 0, str.c_str(), -1, NULL, 0, NULL, 0);
    std::string strResult(cbMultiByte, 0);

    //成功则返回写入到指示的缓冲区的字节数
    size_t nConverted = ::WideCharToMultiByte(CodePage, 0, str.c_str(), (int)str.size(), &strResult[0], (int)strResult.size(), NULL, NULL);

    //调整内容长度
    strResult.resize(nConverted);
    return strResult;
}

std::wstring CStrConvert::_MultiStrToWStr(UINT CodePage, const std::string& str)
{
    //计算缓冲区所需的字符长度
    int cchWideChar = ::MultiByteToWideChar(CodePage, 0, str.c_str(), -1, NULL, 0);
    std::wstring strResult(cchWideChar, 0);

    //成功则返回写入到指示的缓冲区的字符数
    size_t nConverted = ::MultiByteToWideChar(CodePage, 0, str.c_str(), (int)str.size(), &strResult[0], (int)strResult.size());

    //调整内容长度
    strResult.resize(nConverted);
    return strResult;
}

std::string CStrConvert::WStrToAStr(const std::wstring& str)
{
    return _WStrToMultiStr(CP_ACP, str);
}

std::string CStrConvert::WStrToU8Str(const std::wstring& str)
{
    return _WStrToMultiStr(CP_UTF8, str);
}

_tstring CStrConvert::WStrToTStr(const std::wstring& str)
{
#ifdef _UNICODE
    return str;
#else
    return _WStrToMultiStr(CP_ACP, str);
#endif
}

std::wstring CStrConvert::AStrToWStr(const std::string& str)
{
    return _MultiStrToWStr(CP_ACP, str);
}

std::string CStrConvert::AStrToU8Str(const std::string& str)
{
    return WStrToU8Str(AStrToWStr(str));
}

_tstring CStrConvert::AStrToTStr(const std::string& str)
{
#ifdef _UNICODE
    return _MultiStrToWStr(CP_ACP, str);
#else
    return str;
#endif
}

std::wstring CStrConvert::U8StrToWStr(const std::string& str)
{
    return _MultiStrToWStr(CP_UTF8, str);
}

std::string CStrConvert::U8StrToAStr(const std::string& str)
{
    return WStrToAStr(U8StrToWStr(str));
}

_tstring CStrConvert::U8StrToTStr(const std::string& str)
{
#ifdef _UNICODE
    return _MultiStrToWStr(CP_UTF8, str);
#else
    return WStrToAStr(U8StrToWStr(str));
#endif
}

std::string CStrConvert::TStrToAStr(const _tstring& str)
{
#ifdef _UNICODE
    return _WStrToMultiStr(CP_ACP, str);
#else
    return str;
#endif
}

std::wstring CStrConvert::TStrToWStr(const _tstring& str)
{
#ifdef _UNICODE
    return str;
#else
    return AStrToWStr(str);
#endif
}

std::string CStrConvert::TStrToU8Str(const _tstring& str)
{
#ifdef _UNICODE
    return WStrToU8Str(str);
#else
    return WStrToU8Str(AStrToWStr(str));
#endif
}

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值