逆水行船

别人的天堂,是我的异乡;无端的繁华,倍添我的惆怅

 

字符串处理代码(国际化转换C++版)

字符串处理代码主要处理各种字符编码之间的转换。
由unicode转换为utf-8,由utf-8转换为unicode,由unicode转换为ansi、由ansi转换为unicode,由utf-8转换为ansi,由ansi转换为utf-8.
头文件hisString.h代码如下:
#pragma once 

/**
* @defgroup 字符串处理函数
* @brief 主要负责对各种编码字符串之间进行转换,注意:本版本不可以作为dll的导出函数使用,因为用到了string(主要是指MT运行时的dll)。
*    @author 徐敏荣
* @date 2012-07-17
*
* @par 修订历史
* @version v0.5 \n
*    @author 徐敏荣
* @date 2012-07-17
* @li 初始版本
* @{
*/
 

#include <string>
using namespace std;

namespace HisStr
{
    /**
    * @brief 将utf-8编码的字符串转换为unicode编码的字符串。
    * @param[in] str    utf-8编码的字符串
    * @param[out] content    unicode编码的字符串
    * @retval true:成功,false;失败
    */

    bool u82uc(const char* str, wstring& content);

    /**
    * @brief 将unicode编码的字符串转换为utf-8编码的字符串。
    * @param[in] str    unicode编码的字符串
    * @param[out] content    utf-8编码的字符串
    * @retval true:成功,false;失败
    */

    bool uc2u8(const wchar_t* str, string& content);

    /**
    * @brief 将unicode编码的字符串转换为ansi编码的字符串。
    * @param[in] str    unicode编码的字符串
    * @param[out] content    ansi编码的字符串
    * @retval true:成功,false;失败
    */

    bool uc2as(const wchar_t* str, string& content);

    /**
    * @brief 将ansi编码的字符串转换为unicode编码的字符串。
    * @param[in] str    ansi编码的字符串
    * @param[out] content    unicode编码的字符串
    * @retval true:成功,false;失败
    */

    bool as2uc(const char* str, wstring& content);

    /**
    * @brief 将utf-8编码的字符串转换为ansi编码的字符串。
    * @param[in] str    utf-8编码的字符串
    * @param[out] content    ansi编码的字符串
    * @retval true:成功,false;失败
    */

    bool u82as(const char* str, string& content);

    /**
    * @brief 将utf-8编码的字符串转换为unicode编码的字符串。
    * @param[in] str    utf-8编码的字符串
    * @param[out] content    unicode编码的字符串
    * @retval true:成功,false;失败
    */

    bool as2u8(const char* str, string& content);
}

namespace HisStrEx
{
    /**
    * @brief 将utf-8编码的字符串转换为unicode编码的字符串。
    * @param[in] str    utf-8编码的字符串
    * @retval unicode编码的字符串
    * @note 如果失败,返回""
    */

    wstring u82uc(const char* str);

    /**
    * @brief 将unicode编码的字符串转换为utf-8编码的字符串。
    * @param[in] str    ansi编码的字符串
    * @retval utf-8编码的字符串
    * @note 如果失败,返回""
    */

    string uc2u8(const wchar_t* str);

    /**
    * @brief 将utf-8编码的字符串转换为unicode编码的字符串。
    * @param[in] str    utf-8编码的字符串
    * @retval unicode编码的字符串
    * @note 如果失败,返回""
    */

    string uc2as(const wchar_t* str);

    /**
    * @brief 将ansi编码的字符串转换为unicode编码的字符串。
    * @param[in] str    ansi编码的字符串
    * @retval unicode编码的字符串
    * @note 如果失败,返回""
    */

    wstring as2uc(const char* str);

    /**
    * @brief 将utf-8编码的字符串转换为ansi编码的字符串。
    * @param[in] str    utf-8编码的字符串
    * @retval ansi编码的字符串
    * @note 如果失败,返回""
    */

    string u82as(const char* str);

    /**
    * @brief 将ansi编码的字符串转换为utf-8编码的字符串。
    * @param[in] str    ansi编码的字符串
    * @retval utf-8编码的字符串
    * @note 如果失败,返回""
    */

    string as2u8(const char* str);
}

/**//** @}*/ // 字符串处理函数
 
源文件hisString.cpp文件的代码:
#include "hiString.h" 

#include <windows.h>

namespace HisStr
{
    bool u82uc(const char* str, wstring& content)
    {
        int textlen ;
        wchar_t * result;
        textlen = MultiByteToWideChar( CP_UTF8, 0, str,-1, NULL,0 ); 
        result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); 
        ::memset(result,0,(textlen+1)*sizeof(wchar_t)); 
        MultiByteToWideChar(CP_UTF8, 0,str,-1,(LPWSTR)result,textlen); 

        content = result;
        free(result);

        return true
    }

    bool uc2as(const wchar_t* str, string& content)
    {
        char* result;
        int textlen;
        textlen = WideCharToMultiByte(CP_ACP, 0, str, -1, NULL, 0, NULL, NULL );
        result =(char *)malloc((textlen+1)*sizeof(char));
        memset( result, 0, sizeof(char) * ( textlen + 1 ) );
        WideCharToMultiByte( CP_ACP, 0, str, -1, result, textlen, NULL, NULL );

        content = result;
        free(result);

        return true
    }

    bool u82as(const char* str, string& content)
    {
        wstring temp;
        u82uc(str, temp);
        return uc2as(temp.c_str(), content);
    }

    bool as2uc(const char* str, wstring& content)
    {
        int textlen ;
        wchar_t * result;
        textlen = MultiByteToWideChar(CP_ACP, 0, str,-1, NULL,0 ); 
        result = (wchar_t *)malloc((textlen+1)*sizeof(wchar_t)); 
        ::memset(result,0,(textlen+1)*sizeof(wchar_t)); 
        MultiByteToWideChar(CP_ACP, 0,str,-1,(LPWSTR)result,textlen); 

        content = result;
        free(result);
        return true
    }

    bool uc2u8(const wchar_t* str, string& content)
    {
        char* result;
        int textlen;
        textlen = WideCharToMultiByte(CP_UTF8, 0, str, -1, NULL, 0, NULL, NULL );
        result =(char *)malloc((textlen+1)*sizeof(char));
        memset( result, 0, sizeof(char) * ( textlen + 1 ) );
        WideCharToMultiByte( CP_UTF8, 0, str, -1, result, textlen, NULL, NULL );

        content = result;
        free(result);

        return true
    }

    bool as2u8(const char* str, string& content)
    {
        wstring temp;
        as2uc(str, temp);
        return uc2u8(temp.c_str(), content);
    }
}


namespace HisStrEx
{
    wstring u82uc(const char* str)
    {
        wstring temp;
        if (HisStr::u82uc(str, temp))
        {
            return temp;
        }

        return L"";
    }

    string uc2as(const wchar_t* str)
    {
        string temp;
        if (HisStr::uc2as(str, temp))
        {
            return temp;
        }

        return "";
    }

    string u82as(const char* str)
    {
        string temp;
        if (HisStr::u82as(str, temp))
        {
            return temp;
        }

        return "";
    }

    string as2u8(const char* str)
    {
        string temp;
        if (HisStr::as2u8(str, temp))
        {
            return temp;
        }

        return "";
    }

    wstring as2uc(const char* str)
    {
        wstring temp;
        if (HisStr::as2uc(str, temp))
        {
            return temp;
        }

        return L"";
    }

    string uc2u8(const wchar_t* str)
    {
        string temp;
        if (HisStr::uc2u8(str, temp))
        {
            return temp;
        }

        return "";
    }
}

posted on   荣-  阅读(1492)  评论(0编辑  收藏  举报

编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?

导航

统计

点击右上角即可分享
微信分享提示