惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Hacker News: Ask HN
Hacker News: Ask HN
IT之家
IT之家
S
SegmentFault 最新的问题
T
Tailwind CSS Blog
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
J
Java Code Geeks
博客园 - 聂微东
雷峰网
雷峰网
阮一峰的网络日志
阮一峰的网络日志
The Cloudflare Blog
博客园_首页
大猫的无限游戏
大猫的无限游戏
博客园 - 三生石上(FineUI控件)
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园 - 【当耐特】
腾讯CDC
Apple Machine Learning Research
Apple Machine Learning Research
酷 壳 – CoolShell
酷 壳 – CoolShell
V
V2EX
宝玉的分享
宝玉的分享
小众软件
小众软件
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
月光博客
月光博客
NISL@THU
NISL@THU
T
The Exploit Database - CXSecurity.com
C
CXSECURITY Database RSS Feed - CXSecurity.com
WordPress大学
WordPress大学
有赞技术团队
有赞技术团队
Blog — PlanetScale
Blog — PlanetScale
aimingoo的专栏
aimingoo的专栏
L
LINUX DO - 热门话题
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
F
Fortinet All Blogs
博客园 - Franky
L
Lohrmann on Cybersecurity
S
Secure Thoughts
量子位
V
Vulnerabilities – Threatpost
Last Week in AI
Last Week in AI
博客园 - 叶小钗
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
L
LINUX DO - 最新话题
I
InfoQ
C
CERT Recently Published Vulnerability Notes
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Proofpoint News Feed
G
GRAHAM CLULEY
Cisco Talos Blog
Cisco Talos Blog

博客园 - 荣-

学习实践:使用模式,原则实现一个C++自动化测试程序 学习实践:使用模式,原则实现一个C++数据库访问类 C++字符转换等常用方法 DLL内存管理模板类 我的C++数据库访问库--临界区处理类 我的C++数据库访问库 批处理文件的学习 C++中,以类成员函数指针作为参数对std::map中的元素进行迭代处理 取得MySQL数据库表,列信息的SQL语句 我的我的C#数据库操作类(与大家交流) ACE项目的重构整理 安装SQL Server 2000和sp补丁时,安装程序提示"以前的某个程序安装已在安装计算机上创建挂起的文件操作。运行安装程序之前必须重新启动计算机"。 我的计算机安装步骤 创建oracle用户 我的重构步骤 我的C++代码检查列表 PowerDesigner高级应用 文档的阅读 如何设计类
字符串处理代码(国际化转换C++版)
荣- · 2012-07-17 · via 博客园 - 荣-

字符串处理代码主要处理各种字符编码之间的转换。

由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 "";
    }
}