MGJ的地盘

爱拼才会赢!

UTF-8编码与解码 C++版 214

作者为 发表

//utf8字符转码
#include <string>
using namespace std;

string ws2s(const wstring& ws)
{
	string curLocale = setlocale(LC_ALL, NULL); // curLocale = "C";
	setlocale(LC_ALL, "chs"); 
	const wchar_t* _Source = ws.c_str();
	size_t _Dsize = 2 * ws.size() + 1;
	char *_Dest = new char[_Dsize];
	memset(_Dest,0,_Dsize);
	wcstombs(_Dest,_Source,_Dsize);
	string result = _Dest;
	delete []_Dest;
	setlocale(LC_ALL, curLocale.c_str());
	return result;
}
 

wstring s2ws(const string& s)
{
	setlocale(LC_ALL, "chs"); 
	const char* _Source = s.c_str();
	size_t _Dsize = s.size() + 1;
	wchar_t *_Dest = new wchar_t[_Dsize];
	wmemset(_Dest, 0, _Dsize);
	int nret = mbstowcs(_Dest,_Source,_Dsize);
	wstring result = _Dest;
	delete []_Dest;
	setlocale(LC_ALL, "C");
 
	return result;

}


 
wstring UTF2Uni(const char* src, std::wstring &t)
{
	if (src == NULL) 
	{
		return L"";
	}
	
	int size_s = strlen(src);
	int size_d = size_s + 10;          //?
	
	wchar_t *des = new wchar_t[size_d];
	memset(des, 0, size_d * sizeof(wchar_t));
	
	int s = 0, d = 0;
	bool toomuchbyte = true; //set true to skip error prefix.
	
	while (s < size_s && d < size_d)
	{
		unsigned char c = src[s];
		if ((c & 0x80) == 0) 
		{
			des[d++] += src[s++];
		} 
		else if((c & 0xE0) == 0xC0)  ///< 110x-xxxx 10xx-xxxx
		{
			wchar_t *wideChar = &des[d++];
			*wideChar  = (src[s + 0] & 0x3F) << 6;
			*wideChar |= (src[s + 1] & 0x3F);
			
			s += 2;
		}
		else if((c & 0xF0) == 0xE0)  ///< 1110-xxxx 10xx-xxxx 10xx-xxxx
		{
			wchar_t *wideChar = &des[d++];
			
			*wideChar  = (src[s + 0] & 0x1F) << 12;
			*wideChar |= (src[s + 1] & 0x3F) << 6;
			*wideChar |= (src[s + 2] & 0x3F);
			
			s += 3;
		} 
		else if((c & 0xF8) == 0xF0)  ///< 1111-0xxx 10xx-xxxx 10xx-xxxx 10xx-xxxx 
		{
			wchar_t *wideChar = &des[d++];
			
			*wideChar  = (src[s + 0] & 0x0F) << 18;
			*wideChar  = (src[s + 1] & 0x3F) << 12;
			*wideChar |= (src[s + 2] & 0x3F) << 6;
			*wideChar |= (src[s + 3] & 0x3F);
			
			s += 4;
		} 
		else 
		{
			wchar_t *wideChar = &des[d++]; ///< 1111-10xx 10xx-xxxx 10xx-xxxx 10xx-xxxx 10xx-xxxx 
			
			*wideChar  = (src[s + 0] & 0x07) << 24;
			*wideChar  = (src[s + 1] & 0x3F) << 18;
			*wideChar  = (src[s + 2] & 0x3F) << 12;
			*wideChar |= (src[s + 3] & 0x3F) << 6;
			*wideChar |= (src[s + 4] & 0x3F);
			
			s += 5;
		}
	}
	
	t = des;
	delete[] des;
	des = NULL;
	
	return t;
}



int Uni2UTF( const wstring& strRes, char *utf8, int nMaxSize )
{
	if (utf8 == NULL) {
		return -1;
	}
	int len = 0;
	int size_d = nMaxSize;


	for (wstring::const_iterator it = strRes.begin(); it != strRes.end(); ++it)
	{
		wchar_t wchar = *it;
		if (wchar < 0x80)
		{  //
			//length = 1;
			utf8[len++] = (char)wchar;
		}
		else if(wchar < 0x800)
		{
			//length = 2;
			
			if (len + 1 >= size_d)
				return -1;
			
			utf8[len++] = 0xc0 | ( wchar >> 6 );
			utf8[len++] = 0x80 | ( wchar & 0x3f );
		}
		else if(wchar < 0x10000 )
		{
			//length = 3;
			if (len + 2 >= size_d)
				return -1;
			
			utf8[len++] = 0xe0 | ( wchar >> 12 );
			utf8[len++] = 0x80 | ( (wchar >> 6) & 0x3f );
			utf8[len++] = 0x80 | ( wchar & 0x3f );
		}
		else if( wchar < 0x200000 ) 
		{
			//length = 4;
			if (len + 3 >= size_d)
				return -1;
			
			utf8[len++] = 0xf0 | ( (int)wchar >> 18 );
			utf8[len++] = 0x80 | ( (wchar >> 12) & 0x3f );
			utf8[len++] = 0x80 | ( (wchar >> 6) & 0x3f );
			utf8[len++] = 0x80 | ( wchar & 0x3f );
		}
	
	}
	

	return len;
}

string s2utfs(const  string&  strSrc)
{
	string  strRes;
	wstring  wstrUni = s2ws(strSrc);
	
	char*  chUTF8 = new char[wstrUni.length() * 3];
	memset(chUTF8,0x00,wstrUni.length() * 3);
	Uni2UTF(wstrUni,chUTF8, wstrUni.length() * 3);
	strRes = chUTF8;	
	delete  []chUTF8;
	return strRes;
}


string  utfs2s(const string& strutf)
{
	wstring  wStrTmp;
	UTF2Uni( strutf.c_str(),wStrTmp);
	return ws2s(wStrTmp);
}

今天一个点击,将Hotmail升级到Outlook了,页面非常简洁,如果与Win8相结合的话会相当合适。可是想要返回Hotmail界面确实不可能了,想不到Hotmail就这样没落了,难道是Hotmail时代的终结吗?

 

QQ截图20130313195232

当你用“开始”菜单中的“运行”项运行程序时,它会自动记录运行的程序名,以便下次运行时无须再次输入。例如:

但我们有时不想别人看见我们曾经运行过的程序或“运行”项中的历史记录太多了,就需要删除这些历史记录。那怎么做才能删除这些历史记录呢?

有两种方法:

1、打开“任务栏和开始菜单属性”,单击“「开始」菜单”选项卡,然后在“隐私”下,清除“存储并显示最近在「开始」菜单中打开的程序(P)”复选框,然后“确定”即可。如下图:

2、第一种方法是可以把全部的历史记录都清除掉,但你说只想把其中的某项去掉该怎么办呢?那就要用第二种方法了!

我们先要打开注册表,在运行中输入regedit ,找到“HKEY_CURRENT_USER \Software\Microsoft\Windows\CurrentVersion\Explorer\RunMRU”分支,你就可以看见右边栏上有很多项目,你只须把你不要的删去即可!

上面给出的两种方法,大家可以根据自己的实际需要进行选择。

  • 1 • 2
  • 1
  • 2


 主题设计 • skyfrit.com  MGJ的地盘 | 保留所有权利

37 queries in 0.800 seconds.