打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
CString与utf
//string类型的utf-8字符串转为CString类型的unicode字符串CString ConvertUTF8ToCString( std::string utf8str ){    /* 预转换,得到所需空间的大小 */    int nLen = ::MultiByteToWideChar( CP_UTF8, NULL,                      utf8str.data(), utf8str.size(), NULL, 0 );    /* 转换为Unicode */    std::wstring wbuffer;    wbuffer.resize( nLen );    ::MultiByteToWideChar( CP_UTF8, NULL, utf8str.data(), utf8str.size(),                   (LPWSTR) (wbuffer.data() ), wbuffer.length() );#ifdef UNICODE    return(CString( wbuffer.data(), wbuffer.length() ) );#else    /*     * 转换为ANSI     * 得到转换后长度     */    nLen = WideCharToMultiByte( CP_ACP, 0,                    wbuffer.data(), wbuffer.length(), NULL, 0, NULL, NULL );    std::string ansistr;    ansistr.resize( nLen );    /* 把unicode转成ansi */    WideCharToMultiByte( CP_ACP, 0, (LPWSTR) (wbuffer.data() ), wbuffer.length(),                 (LPSTR) (ansistr.data() ), ansistr.size(), NULL, NULL );    return(CString( ansistr.data(), ansistr.length() ) );#endif}
//CString类型的unicode字符串转为string类型的utf-8字符串string _UnicodeToUtf8(CString Unicodestr){        wchar_t* unicode = Unicodestr.AllocSysString();	int len;	len = WideCharToMultiByte(CP_UTF8, 0, unicode, -1, NULL, 0, NULL, NULL);	char *szUtf8 = (char*)malloc(len + 1);	memset(szUtf8, 0, len + 1);	WideCharToMultiByte(CP_UTF8, 0, unicode, -1, szUtf8, len, NULL, NULL);	string result = szUtf8;	free(szUtf8);	return result;}
//string转const char*string str = "abc";const char* result= str.c_str();
//CString转char*CString str;USES_CONVERSION;char * result= T2A(str);
//判断字符是否为UTF-8编码方式,是就返回true,否则返回falsebool _is_str_utf8(string src){    const char* str= src.c_str();	unsigned int nBytes = 0;//UFT8可用1-6个字节编码,ASCII用一个字节	unsigned char chr = *str;	bool bAllAscii = true;	for (unsigned int i = 0; str[i] != '\0'; ++i){		chr = *(str + i);		//判断是否ASCII编码,如果不是,说明有可能是UTF8,ASCII用7位编码,最高位标记为0,0xxxxxxx		if (nBytes == 0 && (chr & 0x80) != 0){			bAllAscii = false;		}		if (nBytes == 0) {			//如果不是ASCII码,应该是多字节符,计算字节数			if (chr >= 0x80) {				if (chr >= 0xFC && chr <= 0xFD){					nBytes = 6;				}				else if (chr >= 0xF8){					nBytes = 5;				}				else if (chr >= 0xF0){					nBytes = 4;				}				else if (chr >= 0xE0){					nBytes = 3;				}				else if (chr >= 0xC0){					nBytes = 2;				}				else{					return false;				}				nBytes--;			}		}		else{			//多字节符的非首字节,应为 10xxxxxx			if ((chr & 0xC0) != 0x80){				return false;			}			//减到为零为止			nBytes--;		}	}	//违反UTF8编码规则	if (nBytes != 0) {		return false;	}	if (bAllAscii){ //如果全部都是ASCII, 也是UTF8		return true;	}	return true;}
//string转CStringstring str = "1234";CString result = CString(str.c_str());
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
Unicode,UTF8互转
字符串和数字之间的转换(Unicode)
UNICODE下CString转换为char*
UTF8到Unicode
char 转wchar_t 及wchar_t转char - 雨点的日志 - 网易博客
CStdioFile.ReadString读取中文产生乱码解决方法
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服