zoukankan      html  css  js  c++  java
  • Qt QString, wchar_t *, TCHAR, CString和其他字符或字符串类型的转化

      1 //QString to wchar_t *:
      2 const wchar_t * encodedName = reinterpret_cast<const wchar_t *>(fileName.utf16());
      3 
      4 //QString to char * given a file name:
      5 QByteArray fileName = QFile::encodeName(aFileName);
      6 const char * encodedName = fileName.constData(); //Valid as long as fileName exists
      7 
      8 //QString to char * (general case):
      9 const char * tmp = str.toUtf8().constData();
     10 [/code]
     11 Windows 数据类型: http://msdn.microsoft.com/en-us/library/aa383751.aspx
     12 [code lang="cpp"]
     13 //TCHAR:
     14 #ifdef UNICODE
     15 typedef wchar_t TCHAR;
     16 #else
     17 typedef char TCHAR;
     18 #endif
     19 
     20 //LPCTSTR:
     21 #ifdef UNICODE
     22 typedef LPCWSTR LPCTSTR;
     23 #else
     24 typedef LPCSTR LPCTSTR;
     25 #endif
     26 
     27 //LPCSTR:
     28 typedef const char * LPCSTR;
     29 
     30 //LPCWSTR:
     31 typedef const wchar_t * LPCWSTR;
     32 
     33 //LPCWSTR to QString:
     34 QString text(QString::fromUtf16(reinterpret_cast<const unsigned short *>(tmp)));
     35 另一种解决办法是使用QString::fromWCharArray(),但这个函数可能导致一些尚未解决的wchar_t符号问题。
     36 
     37 最佳的编程风格: 使用L来定义wchar_t宽字符串,比如 L"text" 字义了一个UNICODE字符串"text" 38 
     39 今天又看到一个文章,关于字符串之间的转换,比较全面,在此将英文翻译并整理一下。
     40 原文地址:http://hi.baidu.com/koko200147/blog/item/7e3cad828c9b9bb66d8119cb.html
     41 
     42 QString与其他字符类型之间的转换,QString在Qt4中是UNICODE编码的,使用utf16规范。
     43 
     44 QString::fromAscii ( const char * str, int size = -1 );
     45 QString::fromLatin1 ( const char * str, int size = -1 );
     46 QString::fromLocal8Bit ( const char * str, int size = -1 );
     47 QString::fromRawData ( const QChar * unicode, int size );
     48 QString::fromStdString ( const std::string & str );
     49 QString::fromStdWString ( const std::wstring & str );
     50 QString::fromUcs4 ( const uint * unicode, int size = -1 );
     51 QString::fromUtf8 ( const char * str, int size = -1 );
     52 QString::fromUtf16 ( const ushort * unicode, int size = -1 );
     53 QString::fromWCharArray ( const wchar_t * string, int size = -1 );
     54 
     55 //qstring ->std::string
     56 QString::toStdString () ;
     57 QString::toStdWString ();
     58 
     59 //BSTR<->QString,不太了解BSTR是什么,还没用到过,所以不知道对不对
     60 BSTR bstr_str;
     61 QString q_str((QChar*)bstr_str, wcslen(bstr_str));
     62 bstr_str = SysAllocString(q_str.utf16());//remember use SysFreeString on BSTR
     63 
     64 //QString<->LPCSTR
     65 QString::toLocal8Bit().constData();
     66 QString::fromLocal8Bit ( const char * str, int size = -1 );
     67 
     68 //QString<->LPCWSTR
     69 QString::utf16();
     70 QString::fromUtf16 ( const ushort * unicode, int size = -1 );
     71 
     72 //QString<->CString
     73 CString c_str(qstring::utf16());
     74 QString fromUtf16 (LPCTSTR(c_str) );
     75 CString转换为char*
     76 
     77 //1.传给未分配内存的const char* (LPCTSTR)指针.
     78 CString cstr(asdd);
     79 const char* ch = (LPCTSTR)cstr;//ch指向的地址和cstr相同。但由于使用const保证ch不会修改,所以安全.
     80 
     81 //2.传给未分配内存的指针.
     82 CString cstr = "ASDDSD";
     83 char *ch = cstr.GetBuffer(cstr1.GetLength() + 1);
     84 cstr.ReleaseBuffer();
     85 //修改ch指向的值等于修改cstr里面的值.
     86 //PS:用完ch后,不用delete ch,因为这样会破坏cstr内部空间,容易造成程序崩溃.
     87 
     88 //3.第二种用法。把CString 值赋给已分配内存的char *。
     89 CString cstr1 = "ASDDSD";
     90 int strLength = cstr1.GetLength() + 1;
     91 char *pValue = new char[strLength];
     92 strncpy(pValue, cstr1, strLength);
     93 
     94 //4.第三种用法.把CString 值赋给已分配内存char[]数组.
     95 CString cstr2 = "ASDDSD";
     96 int strLength1 = cstr1.GetLength() + 1;
     97 char chArray[100];
     98 memset(chArray,0, sizeof(bool) * 100); //将数组的垃圾内容清空.
     99 strncpy(chArray, cstr1, strLength1);
    100 
    101 //5.如果上述都不行,使用以下方法
    102 CString origCString("Hello, World!");
    103 wchar_t* wCharString = origCString.GetBuffer(origCString.GetLength()+1);
    104 size_t origsize = wcslen(wCharString) + 1;
    105 size_t convertedChars = 0;
    106 char *CharString;
    107 CharString=new char(origsize);
    108 wcstombs_s(&convertedChars, CharString, origsize, wCharString , _TRUNCATE);
    109 cout << CharString << endl; //成功输出字符串"Hello,World"
    110 从UTF8编码到GB编码的字符串转换方法:
    111 
    112 QString Utf8_To_GB(QString strText)
    113 {
    114     return QString::fromUtf8(strText.toLocal8Bit().data());
    115 }
    116 从GB编码到UTF8编码的字符串转换方法:
    117 
    118 QString GB_To_Utf8(char *strText)
    119 {
    120     return QString::fromLocal8Bit(strText);
    121 }
  • 相关阅读:
    python 中给文件加锁——fcntl模块
    python生成二维码
    uwsgi常用配置
    php curl实现get和post请求
    python __enter__ 与 __exit__的作用,以及与 with 语句的关系
    python文件操作总结
    Python时间,日期,时间戳之间转换
    Python random模块(获取随机数)
    wigs的理解和应用
    shiro中接入单点登录功能
  • 原文地址:https://www.cnblogs.com/ybqjymy/p/15136585.html
Copyright © 2011-2022 走看看