1. class CTools
  2. {
  3. public:
  4. CTools(void);
  5. public:
  6. ~CTools(void);
  7. public:
  8. static std::string UNICODE_to_UTF8(const CString& unicodeString);
  9. static CString UTF8_to_UNICODE(const std::string& utf8_string);
  10. static std::string ws2s(std::wstring& inputws);
  11. static std::wstring s2ws(const std::string& s);
  12.  
  13. static std::string UNICODE_to_ANSI(const CString& unicodeString);
  14. static CString ANSI_to_UNICODE(const std::string& utf8_string);
  15.  
  16. static std::string GetFullPath(void);
  17. };
  18.  
  19. CTools::CTools(void)
  20. {
  21. }
  22.  
  23. CTools::~CTools(void)
  24. {
  25. }
  26.  
  27. std::string CTools::UNICODE_to_UTF8(const CString& unicodeString)
  28. {
  29. int stringLength = ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, (int)wcslen(unicodeString), NULL, , NULL, NULL);
  30.  
  31. char* buffer = new char[stringLength + ];
  32. ::WideCharToMultiByte(CP_UTF8, NULL, unicodeString, (int)wcslen(unicodeString), buffer, stringLength, NULL, NULL);
  33. buffer[stringLength] = '\0';
  34.  
  35. std::string str = buffer;
  36.  
  37. delete[] buffer;
  38.  
  39. return str;
  40. }
  41.  
  42. CString CTools::UTF8_to_UNICODE(const std::string& utf8_string)
  43. {
  44. int length = (int)utf8_string.size();
  45.  
  46. int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, utf8_string.c_str(), length, NULL, );
  47. wchar_t* wszString = new wchar_t[wcsLen + ];
  48. ::MultiByteToWideChar(CP_UTF8, NULL, utf8_string.c_str(), length, wszString, wcsLen);
  49. wszString[wcsLen] = '\0';
  50. CString unicodeText(wszString);
  51. delete[] wszString;
  52.  
  53. return unicodeText;
  54. }
  55.  
  56. std::string CTools::UNICODE_to_ANSI(const CString& unicodeString)
  57. {
  58. int stringLength = ::WideCharToMultiByte(CP_ACP, NULL, unicodeString, (int)wcslen(unicodeString), NULL, , NULL, NULL);
  59.  
  60. char* buffer = new char[stringLength + ];
  61. ::WideCharToMultiByte(CP_ACP, NULL, unicodeString, (int)wcslen(unicodeString), buffer, stringLength, NULL, NULL);
  62. buffer[stringLength] = '\0';
  63.  
  64. std::string str = buffer;
  65.  
  66. delete[] buffer;
  67.  
  68. return str;
  69. }
  70.  
  71. CString CTools::ANSI_to_UNICODE(const std::string& utf8_string)
  72. {
  73. int length = (int)utf8_string.size();
  74.  
  75. int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, utf8_string.c_str(), length, NULL, );
  76. wchar_t* wszString = new wchar_t[wcsLen + ];
  77. ::MultiByteToWideChar(CP_ACP, NULL, utf8_string.c_str(), length, wszString, wcsLen);
  78. wszString[wcsLen] = '\0';
  79. CString unicodeText(wszString);
  80. delete[] wszString;
  81.  
  82. return unicodeText;
  83. }
  84.  
  85. std::string CTools::ws2s(std::wstring& inputws)
  86. {
  87. return UNICODE_to_UTF8(inputws.c_str());
  88. }
  89.  
  90. std::wstring CTools::s2ws(const std::string& s)
  91. {
  92. CString cstr = UTF8_to_UNICODE(s);
  93. return cstr.GetBuffer(cstr.GetLength());
  94. }
  95.  
  96. std::string CTools::GetFullPath(void)
  97. {
  98. HMODULE h = GetModuleHandle(L"Test.exe");
  99. wchar_t exeFullPath[MAX_PATH]; // MAX_PATH在API中有定义,为128
  100. int len=GetModuleFileName(h,
  101. exeFullPath, //应用程序的全路径存放地址
  102. MAX_PATH);
  103. std::wstring wstrpath = exeFullPath;
  104. std::string strpath = CTools::ws2s(wstrpath);
  105. size_t nPos;
  106. nPos=strpath.rfind('\\');
  107. return strpath.substr(,nPos);
  108. }

工具类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. grunt用来压缩前端脚本

    grunt作为一个任务管理工具,提供丰富的插件和强大的自动化管理功能.需要安装node及npm. 主要使用到两个文件,一个是npm的依赖配置文件package.json { "name&qu ...

  2. [dfs+水] hdu 4462 Scaring the Birds

    题意: N*N的矩阵中有M个点能够放稻草人.且给覆盖距离R 每一个稻草人能覆曼哈顿距离R以内的点 问最少须要多少个稻草人 思路: 由于范围非常小,直接能够暴力 注意稻草人所在的位置是不须要被覆盖的 代 ...

  3. Android 中文API (66) —— BluetoothClass.Device

    前言 本章内容是android.bluetooth.BluetoothClass.Device,为Android蓝牙部分的章节翻译,版本为Android 2.3   r1,翻译来自中山大学的" ...

  4. ie调试控制台

    function initConsole() { var console = document.createElement('div'); console.id = 'errorConsole'; d ...

  5. 最简单的javascript 竖向菜单

    <html xmlns="http://www.w3.org/1999/xhtml"><head runat="server">    ...

  6. SQL Server验证的两种方式

    1.Windows身份验证:本机连接或者受信的局域网连接(一般在忘记管理员密码或者做系统配置的情况下使用). 2.SQLServer验证:使用用户名.密码验证(推荐使用). 启用方法:以Windows ...

  7. 在Windows Server 2008 R2 中架设 SMTP 服务器

    安装SMTP服务器 Step 1 在功能里面勾选SMTP 服务器,一路下一步完成安装 Step 2 在IIS6的SMTP属性里面的访问标签点击连接,然后设置本机可访问. (其实可以不用设置) Step ...

  8. django 基础入门(二)

    一.关于数据库 1.首先django 1.9以上等版本不支持pymysql,因此需要做一些调整. 比如在settings.py 加入一段代码: import pymysql pymysql.insta ...

  9. (C)高级排序法

    1.快速排序法 //方法1 从大到小 #include <iostream.h> void run(int* pData,int left,int right) { int i,j; in ...

  10. Hadoop_Lucene

    http://codelife.me/blog/2012/11/03/jackson-polymorphic-deserialization/ http://itindex.net/blog/2012 ...