C++ 字符编码转换工具类
来源:互联网 发布:小米2a移动网络设置 编辑:程序博客网 时间:2024/05/18 23:53
将网上朋友写的一下工具函数稍作更改封装成了C++类,方便以面向对象的方式编程
头文件:
#ifndef EVNHELPER_H#define EVNHELPER_H#include <string>#include <iostream>using namespace std;class EnvHelper{public:EnvHelper();~EnvHelper();static string UnicodeToANSI( const wstring& str );static wstring UTF8ToUnicode( const string& str );static wstring ANSIToUnicode( const string& str );static string UnicodeToUTF8( const wstring& str );};#endif
源文件:
#include "stdafx.h"#include <windows.h>#include <string>#include <iostream>#include "EnvHelper.h"using namespace std;EnvHelper::EnvHelper(){}EnvHelper::~EnvHelper(){}string EnvHelper::UnicodeToANSI( const wstring& str ){ char* pElementText; int iTextLen; // wide char to multi char iTextLen = WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, NULL, 0,NULL, NULL ); pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) ); ::WideCharToMultiByte( CP_ACP, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL ); string strText; strText = pElementText; delete[] pElementText; return strText;}wstring EnvHelper::UTF8ToUnicode( const string& str ){ int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, NULL, 0 ); wchar_t * pUnicode; pUnicode = new wchar_t[unicodeLen+1]; memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); ::MultiByteToWideChar( CP_UTF8, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen ); wstring rt; rt = ( wchar_t* )pUnicode; delete pUnicode; return rt; }wstring EnvHelper::ANSIToUnicode( const string& str ){ int len = 0; len = str.length(); int unicodeLen = ::MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, NULL, 0 ); wchar_t * pUnicode; pUnicode = new wchar_t[unicodeLen+1]; memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t)); ::MultiByteToWideChar( CP_ACP, 0, str.c_str(), -1, (LPWSTR)pUnicode, unicodeLen ); wstring rt; rt = ( wchar_t* )pUnicode; delete pUnicode; return rt; }string EnvHelper::UnicodeToUTF8( const wstring& str ){ char* pElementText; int iTextLen; // wide char to multi char iTextLen = WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, NULL, 0, NULL, NULL ); pElementText = new char[iTextLen + 1]; memset( ( void* )pElementText, 0, sizeof( char ) * ( iTextLen + 1 ) ); ::WideCharToMultiByte( CP_UTF8, 0, str.c_str(), -1, pElementText, iTextLen, NULL, NULL ); string strText; strText = pElementText; delete[] pElementText; return strText;}
0 0
- 字符编码转换工具
- C++ 字符编码转换工具类
- 字符转换Unicode编码工具
- 字符编码工具类
- 字符编码工具类
- php 的字符编码转换工具
- Linux iconv --字符编码转换工具
- Linux 命令iconv -字符编码转换工具
- linux中字符编码转换工具iconv
- 工具类 编码转换
- 字符转换工具类
- 字符转换工具类
- c/vc字符编码转换解决方案
- C/C++ 字符编码的转换
- C运行库中的字符编码转换
- c/vc字符编码转换解决方案
- c语言实现字符编码转换
- linux c 字符编码转换函数 iconv
- CGI 编程学习
- Fragment里面的Viewpaper不显示内容不执行getItem的解决方案
- uva:Throwing cards away I
- Driver开发常见问题
- Android自定义View之一:初探实例
- C++ 字符编码转换工具类
- 购物系统首页
- Git - 简易指南
- Unity3D入门: 多个摄像机之间的切换
- jquery 的兄弟 父亲 孩子的方法
- NT AUTHORITY\NETWORK SERVICE 权限问题 NT AUTHORITY\NETWORK SERVICE 权限问题
- jetty+xfire实现的webservice接口(maven普通java工程)
- 解决在创建进程流位置是遇到的问题:连接成功,无法检索工作流版本
- 愚人节的爆料 vivo将再一次突破极致?