CString和string在unicode与非unicode下的相互转换(转)

原文转自 http://blog.csdn.net/u014303844/article/details/51397556

CString和string在unicode与非unicode下的相互转换

最近想写一个手机控制电脑的玩具,涉及到了socket通信,数据采用json通用格式,首先是jsoncpp的编译问题太烦了,然后还有更烦的,java中的String多容易的玩意儿,然后到了c/c++/mfc中超级烦,搜索了很久的攻略,用了大把的时间,最后写了个这玩意儿出来,或许可以帮助到一些需要此的道友们哈

string toString(CString cs)
{
#ifdef _UNICODE
    //如果是unicode工程
    USES_CONVERSION;
    std::string str(W2A(cs));
    return str;
#else
    //如果是多字节工程
    std::string str(cs.GetBuffer());
    cs.ReleaseBuffer();
    return str;
#endif // _UNICODE
}

CString toCString(string str)
{
#ifdef _UNICODE
    //如果是unicode工程
    USES_CONVERSION; CString s(str.c_str());
    CString ans(str.c_str());
    return ans;
#else
    //如果是多字节工程
    //string 转 CString
    CString ans;
    ans.Format("%s", str.c_str());
    return ans;
#endif // _UNICODE
}
时间: 2024-08-28 17:02:48

CString和string在unicode与非unicode下的相互转换(转)的相关文章

Unicode环境下完成CString向string类型的转换

CString是MFC的字符串类,它不是基本类型,而是对字符串的封装,它是自适应的,在UNICODE环境下就是CStringW,在非UNICODE环境下就是CStringA. <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

unicode下各种类型转换CString、string

把最近用到的各种unicode下类型转换总结了一下: 1.string转CString string a=”abc”; CString str=CString(a.c_str()); 或str.format("%s", a.c_str()) 2.int转CString Int a; CString Cstr; Cstr.Format(_T("%d"),a); 3.char 转 CString CString.format("%s", char*)

unicode下各种类型转换,CString,string,char*,int,char[]

把最近用到的各种unicode下类型转换总结了一下,今后遇到其他的再补充: 1.string转CString string a=”abc”; CString str=CString(a.c_str()); 或str.format("%s", a.c_str()) 2.int转CString Int a; CString Cstr; Cstr.Format(_T("%d"),a); 3.char 转 CString CString.format("%s&qu

VC2008以资源形式实现多语言版本(非Unicode) .转

http://blog.csdn.net/jiht594/article/details/7043520对比,本文使用了SetThreadLocale. 越来越多的程序支持多语言切换,或者能自动适应当前系统语言,让自己开发的程序支持多语言不仅可以让自己的程序被国人使用,也能让外国程序爱好者使用.VC开发多语言程序有多种方法,或读取配置文件,或使用不同资源DLL等等.这里介绍以资源形式实现多语言版本. 1.打开Visual Studio 2008,新建一个基于对话框工程,名称为LanguageTe

Python中Unicode码和非Unicode码引起的错误与格式转换

1.1. 问题 Problem You need to deal with data that doesn't fit in the ASCII character set. 你需要处理不适合用ASCII字符集表示的数据. 1.2. 解决 Solution Unicode strings can be encoded in plain strings in a variety of ways, according to whichever encoding you choose: Unicode

Unicode字符串和非Unicode字符串

什么是Unicode? Unicode(统一码.万国码.单一码)是计算机科学领域里的一项业界标准,包括字符集.编码方案等.Unicode 是为了解决传统的字符编码方案的局限而产生的,它为每种语言中的每个字符设定了统一并且唯一的二进制编码,以满足跨语言.跨平台进行文本转换.处理的要求. 字符串? 字符串或串(String)是由数字.字母.下划线组成的一串字符.一般记为 s=“a1a2···an”(n>=0).它是编程语言中表示文本的数据类型.在程序设计中,字符串(string)为符号或数值的一个连

SQL 中 unicode字符和非unicode字符的区别

unicode字符就是一些标准的字符,例如英文.数字,不支持汉字. 非unicode是包含汉字和一些特殊字符 nvarchar支持汉字,但是每一个字符占用两个字节比如有这样的字段:[Name] [nvarchar](50) 我们插入“小明”这个记录,只有两个字实际占用4个字节.我们插入“xiaoming”8个英文字符,实际占用16个字节.而[Name] [nvarchar](50) 字段中我们插入“xiaoming”实际只占用8个字节 如果是unicode是可以用varchar非unicode用

CString和string

CString和string(一) 概述 string和CString均是字符串模板类,string为标准模板类(STL)定义的字符串类,已经纳入C++标准之中: CString(typedef CStringT > CString)为Visual C++中最常用的字符串类,继承自CSimpleStringT类,主要应用在MFC和ATL编程中,主要数据类型有char(应用于ANSI),wchar_t(unicode),TCHAR(ANSI与unicode均可): char*为C编程中最常用的字符

Cstring到string

要利用mfc,然后接受一个图片. imread只能读const string& filename 的东西. imread 原型: CV_EXPORTS_W Mat imread( const string& filename, int flags=1 ); 它的参数: filename —— 文件的位置.如果只提供文件名,那么文件应该和C++文件在同一目录,否则必须提供图片的全路径. flags —— 有5个可能的输入. CV_LOAD_IMAGE_UNCHANGED – 在每个通道中,每