2016-05-05 10 views
0

Как преобразовать String в WString.String to Wstring C++ Visual Studio

Прежде чем преобразовать EscapeXML Мне нужно преобразовать первую строку в wstring.

Пример:

У меня есть строка, значение = "& & &", то я хочу, чтобы преобразовать эту строку в wstring.

Мой исходный код:

string escapeXML(const std::string & value) 
{ 
    // Here 
    CA2W ca2w(value.c_str()); 
    wstring value = ca2w; 

    int output_size = EscapeXML(value.c_str(), value.length(), 0, 0, ATL_ESC_FLAG_ATTR); 

    std::wstring testout; 
    testout.resize(output_size); 
    EscapeXML(value.c_str(), value.length(), &testout[0], output_size, ATL_ESC_FLAG_ATTR); 

    using convert_type = std::codecvt_utf8<wchar_t>; 
    std::wstring_convert<convert_type, wchar_t> converter; 

    string converted_str = converter.to_bytes(testout); 

    return converted_str; 
} 

int main() 
{ 

std::string test = " & & & "; 
cout << escapeXML(test) << '/n'; 

    return 0; 
} 

Мой выход:

C2082: redefinition of formal parameter 'value' 
IntelliSense: argument of type "const char *" is incompatible with parameter of type "const wchar_t *" 
+0

Не запуская свой код, я не вижу вашу проблему/вопрос? Что вы ожидали, что вы узнали? –

+0

Я обновляю теперь случай. Надеюсь, вы поймете проблему :) – Skydreampower

+0

Спасибо за ваш совет @computerfreaker. – Skydreampower

ответ

0

теперь я нашел проблему. Проблема заключается в дублировании значений.

У меня есть строка, называемая значением, а в wstring называется значением. Этот способ не должен работать. Я меняю имя, это решение. Благодарю.

#include "stdafx.h" 
#include "atlenc.h" 
#include <string> 
#include <iostream> 
#include <codecvt> 
using namespace std; 


string escapeXML(const std::string & value) 
{ 
    CA2W ca2w(value.c_str()); 
    wstring value2 = ca2w; 
    int output_size = EscapeXML(value2.c_str(), value.length(), 0, 0, ATL_ESC_FLAG_ATTR); 

    std::wstring testout; 
    testout.resize(output_size); 
    EscapeXML(value2.c_str(), value2.length(), &testout[0], output_size, ATL_ESC_FLAG_ATTR); 

    using convert_type = std::codecvt_utf8<wchar_t>; 
    std::wstring_convert<convert_type, wchar_t> converter; 

    string converted_str = converter.to_bytes(testout); 

    return converted_str; 
} 



int main() 
{ 

    std::string test = " Bayern & Münschen"; 
    std::string test2 = " \" \" \" "; 
    std::string test3 = " ' ' ' "; 
    std::string test4 = "< < <"; 
    std::string test5 = " > > >"; 

    cout << escapeXML(test); 
    cout << escapeXML(test2) << '/n'; 
    cout << escapeXML(test3) << '/n'; 
    cout << escapeXML(test4) << '/n'; 
    cout << escapeXML(test5) << '/n'; 

    return 0; 
} 

 Смежные вопросы

  • Нет связанных вопросов^_^