class CTools
{
public:
CTools(void);
public:
~CTools(void);
public:
static std::string UNICODE_to_UTF8(const CString& unicodeString);
static CString UTF8_to_UNICODE(const std::string& utf8_string);
static std::string ws2s(std::wstring& inputws);
static std::wstring s2ws(const std::string& s); static std::string UNICODE_to_ANSI(const CString& unicodeString);
static CString ANSI_to_UNICODE(const std::string& utf8_string); static std::string GetFullPath(void);
}; CTools::CTools(void)
{
} CTools::~CTools(void)
{
} std::string CTools::UNICODE_to_UTF8(const CString& unicodeString)
{
int stringLength = ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, (int)wcslen(unicodeString), NULL, , NULL, NULL); char* buffer = new char[stringLength + ];
::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, (int)wcslen(unicodeString), buffer, stringLength, NULL, NULL);
buffer[stringLength] = '\0'; std::string str = buffer; delete[] buffer; return str;
} CString CTools::UTF8_to_UNICODE(const std::string& utf8_string)
{
int length = (int)utf8_string.size(); int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, utf8_string.c_str(), length, NULL, );
wchar_t* wszString = new wchar_t[wcsLen + ];
::MultiByteToWideChar(CP_UTF8, NULL, utf8_string.c_str(), length, wszString, wcsLen);
wszString[wcsLen] = '\0';
CString unicodeText(wszString);
delete[] wszString; return unicodeText;
} std::string CTools::UNICODE_to_ANSI(const CString& unicodeString)
{
int stringLength = ::WideCharToMultiByte(CP_ACP, NULL, unicodeString, (int)wcslen(unicodeString), NULL, , NULL, NULL); char* buffer = new char[stringLength + ];
::WideCharToMultiByte(CP_ACP, NULL, unicodeString, (int)wcslen(unicodeString), buffer, stringLength, NULL, NULL);
buffer[stringLength] = '\0'; std::string str = buffer; delete[] buffer; return str;
} CString CTools::ANSI_to_UNICODE(const std::string& utf8_string)
{
int length = (int)utf8_string.size(); int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, utf8_string.c_str(), length, NULL, );
wchar_t* wszString = new wchar_t[wcsLen + ];
::MultiByteToWideChar(CP_ACP, NULL, utf8_string.c_str(), length, wszString, wcsLen);
wszString[wcsLen] = '\0';
CString unicodeText(wszString);
delete[] wszString; return unicodeText;
} std::string CTools::ws2s(std::wstring& inputws)
{
return UNICODE_to_UTF8(inputws.c_str());
} std::wstring CTools::s2ws(const std::string& s)
{
CString cstr = UTF8_to_UNICODE(s);
return cstr.GetBuffer(cstr.GetLength());
} std::string CTools::GetFullPath(void)
{
HMODULE h = GetModuleHandle(L"Test.exe");
wchar_t exeFullPath[MAX_PATH]; // MAX_PATH在API中有定义,为128
int len=GetModuleFileName(h,
exeFullPath, //应用程序的全路径存放地址
MAX_PATH);
std::wstring wstrpath = exeFullPath;
std::string strpath = CTools::ws2s(wstrpath);
size_t nPos;
nPos=strpath.rfind('\\');
return strpath.substr(,nPos);
}

工具类CTools实现字符编码转换和获取当前路径的更多相关文章

  1. Char Tools,方便的字符编码转换小工具

    工作关系,常有字符编码转换方面的需要,写了这个小工具 Char Tools是一款方便的字符编码转换小工具,基于.Net Framework 2.0 Winform开发 主要功能 URL编码:URLEn ...

  2. 编码问题 php字符编码转换类

    各种平台和软件打开显示的编码问题,需要使用不同的编码,根据我们不同的需求. php 字符编码转换类,支持ANSI.Unicode.Unicode big endian.UTF-8.UTF-8+Bom ...

  3. iconv字符编码转换

    转自 http://blog.csdn.net/langresser_king/article/details/7459367 iconv(http://www.gnu.org/software/li ...

  4. php字符编码转换之gb2312转为utf8(转)

    在php中字符编码转换我们一般会用到iconv与mb_convert_encoding进行操作,但是mb_convert_encoding在转换性能上比iconv要差很多哦.string iconv ...

  5. php 字符编码转换函数 iconv mb_convert_encoding比较

    在使用PHP处理字符串时,我们经常会碰到字符编码转换的问题,你碰到过iconv转换失败吗? 发现问题时,网上搜了搜,才发现iconv原来有bug ,碰到一些生僻字就会无法转换,当然了配置第二个参数时, ...

  6. Python—字符编码转换、函数基本操作

    字符编码转换 函数 #声明文件编码,格式如下: #-*- coding:utf-8 -*- 注意此处只是声明了文件编码格式,python的默认编码还是unicode 字符编码转换: import sy ...

  7. day4学python 字符编码转换+元组概念

    字符编码转换+元组概念 字符编码转换 #coding:gbk //此处必声明 文件编码(看右下角编码格式) #用来得到python默认编码 import sys print(sys.getdefaul ...

  8. erlang中字符编码转换(转)

    转自:http://www.thinksaas.cn/group/topic/244329/ 功能说明: erlang中对各种语言的编码支持不足,此代码是使用erlang驱动了著名的iconv编码库来 ...

  9. Qt代码区字符编码转换

    在做通讯练习的时候,发现发送给小助手字符乱码,图片如下 本人Qt Creator是UTF-8,需要改成gbk,代码如下 #include<QTextCodec> // 提供字符编码转换 Q ...

随机推荐

  1. 入门Html

    html 超文本标记语言(Hypertext Markup Language),用于描写网页文档的标记语言,最新的版本为5.0.由万维网制定 和更新,其实它本质还是文本文件,只不过需要浏览器来解释显示 ...

  2. POJ1797 Heavy Transportation 【Dijkstra】

    Heavy Transportation Time Limit: 3000MS   Memory Limit: 30000K Total Submissions: 21037   Accepted:  ...

  3. D3DXMatrixMultiply 函数

    D3DXMatrixMultiply 函数 两个矩阵相乘. 定义:D3DXMATRIX *WINAPI D3DXMatrixMultiply(           D3DXMATRIX *pOut,  ...

  4. UVA 10312 - Expression Bracketing(数论+Catalan数)

    题目链接:option=com_onlinejudge&Itemid=8&page=show_problem&problem=1253">10312 - Exp ...

  5. python之列表生成式

    列表生成式即List Comprehensions,是Python内置的非常简单却强大的可以用来创建list的生成式. 1,比如:要生成list [1, 2, 3, 4, 5, 6, 7, 8, 9, ...

  6. 在一个数组中是否存在两个数A、B的和为M

    #include <iostream>#include <algorithm>//#include <vector>using namespace std; int ...

  7. 使用yum来下载RPM包而不进行安装

    1. 安装yum-downloadonly. yum-utils 或 yum-plugin-downloadonly 软件包 (RHEL5) # yum install yum-downloadonl ...

  8. Laravel 5.1 ACL权限控制 四 之middleware

    1.创建Middleware php artisan make:middleware MustBeAnAdmin 2.实现 MustBeAnAdmin.php中的handle方法,判断登录的用户是否为 ...

  9. JS 获取星期几的四种写法

    今天是星期几的4种JS代码写法,有需要的朋友可以参考一下 第一种写法 复制代码代码如下: var str = "";  var week = new Date().getDay() ...

  10. Python学习之day4

    参考文献:http://www.cnblogs.com/alex3714/articles/5143440.html 迭代器: 迭代器是访问集合元素的一种方式.迭代器对象从集合的第一个元素开始访问,直 ...