CString, QString, char*之间的转换(包括VC编译开关)
传给未分配内存的const char* (LPCTSTR)指针.
CString cstr(asdd);
const char* ch = (LPCTSTR)cstr;
ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.2.传给未分配内存的指针.
CString cstr = "ASDDSD";
char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
cstr.ReleaseBuffer();
//修改ch指向的值等于修改cstr里面的值.
//PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.
3.第二种用法。把CString 值赋给已分配内存的char *。
CString cstr1 = "ASDDSD";
int strLength = cstr1.GetLength() + 1;
char *pValue = new char[strLength];
strncpy(pValue, cstr1, strLength);
4.第三种用法.把CString 值赋给已分配内存char[]数组.
CString cstr2 = "ASDDSD";
int strLength1 = cstr1.GetLength() + 1;
char chArray[100];
memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.
strncpy(chArray, cstr1, strLength1);
如果上述都不行:
CString转换为char*
CString origCString("Hello, World!");
wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
size_t origsize = wcslen(wCharString) + 1;
size_t convertedChars = 0;
char *CharString;
CharString=new char(origsize);
wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
cout << CharString << endl;
成功输出字符串"Hello,World"
原因:
原来在VC++ 2005以前,应用程序默认都是关闭对Unicode的支持的,而在VC2005中,默认打开了对它的支持,CString对应的字符串应该是TCHAR,TCHAR的定义是这样的,
#ifdef _UNICODE
typedef wchar_t TCHAR ;
#else
typedef char TCHAR;
#endif
所以在工程中应该可以关闭对于Unicode的支持,从而可以直接转换。这个做法是右击工程名—〉Property—〉General中的character set中选择notset,这样,本文开头的那段代码就可以正确的执行了。
如何将QString转换为char *或者相反
How can I convert a QString to char* and vice versa ?(trolltech)
Answer:
In order to convert a QString to a char*, then you first need to get a latin1 representation of the string by calling toLatin1() on it which will return a QByteArray. Then call data() on the QByteArray to get a pointer to the data stored in the byte array. See the documentation:
See the following example for a demonstration:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QString str1 = "Test";
QByteArray ba = str1.toLatin1();
const char *c_str2 = ba.data();
printf("str2: %s", c_str2);
return app.exec();
}
Note that it is necessary to store the bytearray before you call data() on it, a call like the following
const char *c_str2 = str2.toLatin1().data();
will make the application crash as the QByteArray has not been stored and hence no longer exists.
To convert a char* to a QString you can use the QString constructor that takes a QLatin1String, e.g:
QString string = QString(QLatin1String(c_str2)) ;
还有其他多种方法:
方法一 -----------------------------------------
#define G2U(s) ( QTextCodec::codecForName("GBK")->toUnicode(s) )
#define U2G(s) ( QTextCodec::codecForName("GBK")->fromUnicode(s) )
QString str;
QCString cstr;
str = G2U("中文输入");
cstr = U2G(str);
QCString有这样一个重载运算符
operator const char * () const
可以这样
printf("%s\n", (const char*) cstr);
或是copy出来
char buf[1024];
strcpy(buf, (const char*) cstr);
方法二 -----------------------------------------
如果是中文系统
直接用 (const char*) str.local8Bit()
例如
printf("%s", (const char*) str.local8Bit());
str是一个QString
方法三 -----------------------------------------
char str[64];
QTextCodec *textcod = QTextCodec::codecForName("GBK");
QCString string1 = textcod ->fromUnicode(listbox1->currentText());
strcpy(str,string1);
QString和Std::string
从char*到 QString可以从fromLocal8Bit()转化
std::string有c_str()的函数使再转化为char*
QString有toAscii()记不清了你可以看看.又是我的粗心酿成大错,我重新查看了一下Qt文档,原来Qt可以直接从std::wstring产生一个QString,用QString::fromStdWString(const std::wstring &)这个静态成员函数即可。我试了试用std::string的c_str()返回的char *构造的QString不能再保存原先的中文信息,而用std::wstring构造的QString则可以用qDebug()输出原先的中文信息
GB编码与UTF8编码的转换
在主函数app后加上这句:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));
然后是从UTF8编码到GB编码的字符串转换方法:
QString Utf8_To_GB(QString strText)
{
return QString::fromUtf8(strText.toLocal8Bit().data());
}
至于从GB到UTF8,那大家就经常用了:
QString GB_To_Utf8(char *strText)
{
return QString::fromLocal8Bit(strText);
}
参考:http://www.cppblog.com/Alina-zl/archive/2008/11/19/67323.html
CString, QString, char*之间的转换(包括VC编译开关)的更多相关文章
- CString,string,char*之间的转换(转)
这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌握的:char*是从学习C语 ...
- 【转载】CString,string,char*之间的转换
本文转自 <> 这三种类型各有各的优点,比如CString比较灵活,是基于MFC常用的类型,安全性也最高,但可移植性最差.string是使用STL时必不可少的类型,所以是做工程时必须熟练掌 ...
- mfc CString,string,char* 之间的转换
知识点: CString转char*,string string转char*,CString char* 转CString,string 一.CString转char*,string //字串转换测试 ...
- CString与 char *之间的转换
http://www.cnblogs.com/watsonlong/archive/2011/04/15/2017086.html
- VC CString,int,string,char*之间的转换
CString转string : CString strMfc = "test"; std::string strStr; strStr = strMfc.GetBuffer(); ...
- (转)CString,int,string,char*之间的转换
CString,int,string,char*之间的转换http://www.cnblogs.com/greatverve/archive/2010/11/10/cstring-int-string ...
- MFC/C++/C中字符类型CString, int, string, char*之间的转换
1 CString,int,string,char*之间的转换 string 转 CString CString.format("%s", string.c_str()); cha ...
- char*,string,char a[], const char *,之间的转换
1. const char* 和string 转换 (1) const char*转换为 string,直接赋值即可. EX: const char* tmp = "tsinghu ...
- char*,wchar_t*,CString和BSTR之间的转换
前言 本文并不尝试列举出所有的转换方法,只列举作者认为方便易用的方法. 1.char*和wchar_t*的相互转换 可以利用中间类_bstr_t(头文件comdef.h)方便的进行相互转换 const ...
随机推荐
- R简易入门(一)
本文内容来源:https://www.dataquest.io/mission/126/introduction-to-r 本文数据来源:https://www.whitehouse.gov/21st ...
- hdu 5755 2016 Multi-University Training Contest 3 Gambler Bo 高斯消元模3同余方程
http://acm.hdu.edu.cn/showproblem.php?pid=5755 题意:一个N*M的矩阵,改变一个格子,本身+2,四周+1.同时mod 3;问操作多少次,矩阵变为全0.输出 ...
- 【Ibatis】总结各种使用技巧
[Ibatis]总结各种使用技巧 <alias> <typeAlias alias="YintaiMobile_FreeData_Model" type=&quo ...
- ASP.NET MVC +EasyUI 权限设计(三)基础模块
请注明转载地址:http://www.cnblogs.com/arhat 在上一章中呢,我们基本上搭建好了环境,那么本章我们就从基础模块开始写起.由于用户,角色,动作三个当中,都是依赖与动作的,所以本 ...
- C#的winform拼数字游戏
C#的winform拼数字游戏 声明:阅读了别人的代码学习修改而来,增加了美观度和游戏乐趣.(作者出处忘了不好意思) 程序截图 关键代码 using System; using System.Coll ...
- Swift和OC,是编译型语言、解释性语言、运行时语言
首先需要明确的一点是,什么是编译型语言和解释性语言 编译型语言,就是在其执行过程中需要先将其经过编译成机器码来给计算机识别的,其执行效率就会比较高这个是显而易见的,常见比如:C.C++ 而解释型语言, ...
- XCODE真机调试设备连接一直忙碌如何处理
只是还没反应过来 等一会就行了
- iOS10 关于推送-b
最近在研究iOS10关于推送的新特性, 相比之前确实做了很大的改变,总结起来主要是以下几点: 推送内容更加丰富,由之前的alert 到现在的title, subtitle, body 推送统一由tri ...
- beego 0.9.0 中智能路由AutoRouter的使用方法及源码解读
了解beego的开发者肯定知道,beego的路由设计来源于sinatra,原来是不支持自动路由的,每一个路由都要自己配置的,如: type MainController struct { beego. ...
- 2005: [Noi2010]能量采集 - BZOJ
Description 栋栋有一块长方形的地,他在地上种了一种能量植物,这种植物可以采集太阳光的能量.在这些植物采集能量后,栋栋再使用一个能量汇集机器把这些植物采集到的能量汇集到一起. 栋栋的植物种得 ...