CString, QString, char*之间的转换

这篇博客探讨了在VC++环境中,如何在CString, QString和char*之间进行转换。文章提到了不同方法,包括利用const char*、toLatin1()、QString的构造函数等,并特别指出在Unicode支持的问题上,需要设置工程属性来避免问题。同时,还提供了QString与std::string之间的转换技巧,以及GB编码和UTF8编码的转换方法。" 137542388,9777775,S7-1500F与S7-1200F安全PLC安全通信程序示例,"['S7-1500F', 'S7-1200F', '安全PLC', '控制器与智能设备PN通信', '程序示例']

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.传给未分配内存的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 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中,默认打开了对它的支持,Qsring对应的字符串应该是TCHAR,TCHAR的定义是这样的,

#ifdef _UNICODE
typedef wchar_t TCHAR ;
#else
typedef char TCHAR;
#endif 

所以在工程中应该可以关闭对于Unicode的支持,从而可以直接转换。这个做法是右击工程名—〉Property—〉Deneral中的characterset中选择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后加上这句:

QUOTE:
QTextCodec::setCodecForLocale(QTextCodec::codecForName("GB18030"));

然后是从UTF8编码到GB编码的字符串转换方法:

QUOTE:

QString Utf8_To_GB(QString strText)
{
  return QString::fromUtf8(strText.toLocal8Bit().data());
}

至于从GB到UTF8,那大家就经常用了:

QUOTE:
QString GB_To_Utf8(char *strText)
{
    return QString::fromLocal8Bit(strText);
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值