2011-11-18 3 views
1

tcp를 통해 인코딩 된 유니 코드를 사용하여 런타임 문자열을받는 응용 프로그램을 작성하고 있습니다. 예제 문자열은 "\ u7cfb \ u8eca \ u4e21 \ uff1a \ u6771 \ u5317 ..."입니다. 나는 다음과 같은 있지만 불행히도 때문에 만날 수있는 컴파일 시간 : 불완전한 보편적 인 문자 이름 \ u는 그 때부터 컴파일 시간에 기대하는 4 16 진수 문자. 내가 런타임에 대한 해결책을 모색하고있어런타임 유니 코드 문자열 복원

QString restoreUnicode(QString strText) 
    { 
     QRegExp rx("\\\\u([0-9a-z]){4}"); 
     return strText.replace(rx, QString::fromUtf8("\u\\1")); 
    } 

, 나는 나는이 문자열을 끊고 후 그 헥사 값을 변환하는 몇 가지 조작을 예견 할 수있는 "u는 \"기본 다음 10로 분리 문자의 생성자로 전달 QChar하지만 나는 그러한 방법으로 발생하는 시간 복잡성에 대해 매우 우려하고 전문가는 아니기 때문에 나는 더 좋은 방법을 찾고 있습니다.

해결책이나 요령이있는 사람이 있습니까?

+1

'fromUtf8 ("\\ u \\ 1")'wor 케이? 당신의 생각은 다음과 같은 문제가 있습니다 :'const char razy [] = "lass"; 미친 푸 (int a; 부울 b; };' –

+0

왜 소켓을 통해 데이터를 인코딩/디코딩하는'QDataStream'을 사용하지 않습니까? –

+0

서버를 제어 할 권한이 없습니다. 제 3 자 데이터 스트림은 ascii와 약간의 경우가 포함 된 유니 코드가 섞여 있기 때문에 작업 할 수 있습니다. 나는 잘 작동하는 해결책을 만들었고, 자신의 질문 타이머가이 사이트에서 만료 될 때 6 시간 만에 게시 할 것입니다. – Will

답변

1

, 여기에 범위를 최적화하기 전에 내 최초의 솔루션이다 이 변수의 팬은 아니지만 유니 코드 존재 여부는 낮지 만, 제어 할 수없는 스트림에서 유니 코드 및/또는 ascii의 예측할 수없는 성격을 감안할 때 작동합니다. 대신 그것을 처리하는 것이 좋습니다. 못생긴 \ u1234 등.

QString restoreUnicode(QString strText) 
{ 
    QRegExp rxUnicode("\\\\u([0-9a-z]){4}"); 

    bool bSuccessFlag; 
    int iSafetyOffset = 0; 
    int iNeedle = strText.indexOf(rxUnicode, iSafetyOffset); 

    while (iNeedle != -1) 
    { 
     QChar cCodePoint(strText.mid(iNeedle + 2, 4).toInt(&bSuccessFlag, 16)); 

     if (bSuccessFlag) 
      strText = strText.replace(strText.mid(iNeedle, 6), QString(cCodePoint)); 
     else 
      iSafetyOffset = iNeedle + 1; // hop over non code point to avoid lock 

     iNeedle = strText.indexOf(rxUnicode, iSafetyOffset); 
    } 

    return strText; 
} 
1

문자열을 직접 해독해야합니다. 그냥 (wchar_t)result로 원래 문자열 \\uXXXX을 (int result; std::istringstream iss(s); if (!(iss>>std::hex>>result).fail()) ...를 구문 분석, 유니 코드 항목 (rx.indexIn(strText))를 타고 교체하십시오. 폐쇄와 미래에이 스레드를 통해 오는 사람들을위한

+0

나는 그것이 잘 작동하는 것과 비슷한 일을 해왔다. 유니 코드의 존재가 낮아서 내 솔루션이 문제가되는 CPU 사용을 가져 오지 않기를 바란다. 사이트에서 나를 허용하면 6 시간 안에 해결책을 게시 할 것입니다. – Will

1
#include <assert.h> 
#include <iostream> 
#include <string> 
#include <sstream> 
#include <locale> 
#include <codecvt>   // C++11 
using namespace std; 

int main() 
{ 
    char const data[] = "\\u7cfb\\u8eca\\u4e21\\uff1a\\u6771\\u5317"; 

    istringstream stream(data); 

    wstring  ws; 
    int   code; 
    char  slashCh, uCh; 
    while(stream >> slashCh >> uCh >> hex >> code) 
    { 
     assert(slashCh == '\\' && uCh == 'u'); 
     ws += wchar_t(code); 
    } 

    cout << "Unicode code points:" << endl; 
    for(auto it = ws.begin(); it != ws.end(); ++it) 
    { 
     cout << hex << 0 + *it << endl; 
    } 
    cout << endl; 

    // The following is C++11 specific. 
    cout << "UTF-8 encoding:" << endl; 
    wstring_convert< codecvt_utf8<wchar_t> > converter; 
    string const bytes = converter.to_bytes(ws); 
    for(auto it = bytes.begin(); it != bytes.end(); ++it) 
    { 
     cout << hex << 0 + (unsigned char)*it << ' '; 
    } 
    cout << endl; 
} 
+0

스트림은 독점적으로 유니 코드가 아니며 진행중인 스트림의 문자열 내에 유니 코드가 아닌 항목이있을 수 있지만 감사합니다. – Will