c++编写字符串编码类

     废话不多说,上代码:

#include <iostream>
#include <string>
#include <windows.h>

using namespace std;
class CEncodeString
{
public:
	CEncodeString();
	~CEncodeString();
	void LoadString(char *,int =ansi);
	void LoadString(WCHAR *);
	enum{ansi,unicode,utf8};
	wstring ToUnicode();
	string ToAnsi();
	string ToUtf8();
	string GetUnicodeHeader();
	string GetUtf8Header();
private:
	string m_str;
};
CEncodeString::CEncodeString()
{

}
CEncodeString::~CEncodeString()
{

}
void CEncodeString::LoadString(char *str,int mode/* =ansi */)
{
	if(mode==ansi)
	{
		string strTemp;
		strTemp+=str[0];
		strTemp+=str[1];
		if(strTemp==GetUnicodeHeader())
		{
			LoadString((WCHAR*)(str+2));
			return;
		}
		strTemp+=str[2];
		if(strTemp==GetUtf8Header())
		{
			str+=3;
			mode=utf8;
		}
		else
		{
			m_str=str;
		}
	}
	if(mode==utf8)
	{
		int wcsLen = ::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), NULL, 0); 
		wchar_t* wszString = new wchar_t[wcsLen + 1]; 
		::MultiByteToWideChar(CP_UTF8, NULL, str, strlen(str), wszString, wcsLen); 
		wszString[wcsLen] = '\0'; 
		int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); 
		char* szAnsi = new char[ansiLen + 1]; 
		::WideCharToMultiByte(CP_ACP, NULL, wszString, wcslen(wszString), szAnsi, ansiLen, NULL, NULL); 
		szAnsi[ansiLen] = '\0'; 
		m_str=szAnsi;
		delete[] wszString;
		delete[] szAnsi;
	}
}
void CEncodeString::LoadString(WCHAR *wStr)
{
	int ansiLen = ::WideCharToMultiByte(CP_ACP, NULL, wStr, wcslen(wStr), NULL, 0, NULL, NULL); 
	char* szAnsi = new char[ansiLen + 1]; 
	::WideCharToMultiByte(CP_ACP, NULL, wStr, wcslen(wStr), szAnsi, ansiLen, NULL, NULL); 
	szAnsi[ansiLen] = '\0'; 
	m_str=szAnsi;
	delete[] szAnsi;
}
wstring CEncodeString::ToUnicode()
{
	char* szAnsi =(char*)m_str.data(); 
    int wcsLen = ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), NULL, 0); 
    wchar_t* wszString = new wchar_t[wcsLen + 1]; 
    ::MultiByteToWideChar(CP_ACP, NULL, szAnsi, strlen(szAnsi), wszString, wcsLen); 
    wszString[wcsLen] = '\0'; 
	wstring wStr=wszString;
	delete[] wszString;
	return wStr;
}
string CEncodeString::ToAnsi()
{
	return m_str;
}
string CEncodeString::ToUtf8()
{
	wstring wStr=ToUnicode();
	wchar_t* wszString =(wchar_t*) wStr.data(); 
    int u8Len = ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), NULL, 0, NULL, NULL); 
    char* szU8 = new char[u8Len + 1]; 
    ::WideCharToMultiByte(CP_UTF8, NULL, wszString, wcslen(wszString), szU8, u8Len, NULL, NULL); 
    szU8[u8Len] = '\0'; 
	string strUtf8=szU8;
	delete[] szU8;
	return strUtf8;
}
string CEncodeString::GetUnicodeHeader()
{
	return "\xff\xfe";
}
string CEncodeString::GetUtf8Header()
{
	return "\xef\xbb\xbf";
}
main()
{
	setlocale(LC_CTYPE, "");  
	FILE *f=fopen("d:\\json.txt","rb");
	char buf[100]={0};
	fread(buf,1,100,f);
	fclose(f);
	CEncodeString encode;
	encode.LoadString(buf);
	cout<<encode.ToAnsi()<<endl;
}


      这个类会自动解析字符串的头信息,判断是否为unicode还是utf8还是ansi,然后会做相应的处理,当然字符串在类中的储存方式都是ansi的。

      本文由不足之处,还望大家多多指正。

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
自己实现的字符串 class CMStringImp; class CMstring { public: explicit CMstring(void); ~CMstring(void); CMstring(LPCTSTR lpszstr); CMstring(const CMstring& lpszstr); CMstring& operator = (const CMstring& lpszstr); operator LPCTSTR() const; bool operator == (const CMstring&) const; bool operator != (const CMstring&) const; bool operator < (const CMstring&) const; TCHAR operator[] (int nIndex) const; TCHAR& operator[] (int nIndex); CMstring& operator += (LPCTSTR pStr); CMstring& operator += (TCHAR ch); friend CMstring operator+(const CMstring& str1, const CMstring& str2); friend CMstring operator+(const CMstring& str1, LPCTSTR lpszstr); friend CMstring operator+(const CMstring& str1, TCHAR ch); friend CMstring operator+(TCHAR ch, const CMstring& str1); friend CMstring operator+(LPCTSTR lpszstr, const CMstring& str1); // friend wostream operator <<(wostream &is;,const CMstring &str;); public: LPCTSTR GetData() const; bool IsEmpty() const; TCHAR GetAt(int) const; TCHAR& GetAt(int); int GetLength() const; int Compare(const CMstring&) const; int CompareNoCase(const CMstring&) const; int Find(TCHAR ch, int nStart = 0) const; int Find(LPCTSTR pStr, int nStart = 0) const; int ReverseFind(TCHAR ch) const; int ReverseFind(LPCTSTR pStr) const; CMstring Right(int nCount) const; CMstring Left(int nCount ) const; public: CMstring& MakeLower(); CMstring& MakeUpper(); CMstring& MakeReverse(); int Replace(TCHAR chOld, TCHAR chNew); int Replace(LPCTSTR pszOld, LPCTSTR pszNew); int Insert(int iIndex, TCHAR ch); void Format(LPCTSTR lpszFormat, ...); private: CMStringImp* m_pImp; };

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值