-2

Possible Duplicate:
Compare std::wstring and std::string

I have silly question. I know I can use L prefix before a string to use it as wchar_t* (for unicode strings) but I dont know how to use this prefix before variable. I mean

std::wstring str = L"hello";

I know the code above, but how about this one:

string somefunction();

std::wstring str1 = L(somfunction()) 

this say that 'L' identifier not found

the problem is how to apply L prefix to unquoted string?

void wordNet::extractWordIds(wstring targetWord)
{

    pugi::xml_document doc;
    std::ifstream stream("words0.xml");
    pugi::xml_parse_result result = doc.load(stream);
    pugi::xml_node words = doc.child("Words");

    for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
    {       
        std::string wordValue =  as_utf8(it->child("WORDVALUE").child_value());
        std::wstring result (wordValue.size (), L' ');
        std::copy (wordValue.begin (), wordValue.end (), result.begin ()); 
        if(!result.compare(targetWord))
            cout << "found!" << endl; 
    }


}

actully I want to compare targetWord with wordValue. you see that I convert wordValue to wstring but still dont get the right result by comparision.

Community
  • 1
  • 1
aliakbarian
  • 709
  • 1
  • 11
  • 20
  • 2
    Try googling "convert string to wstring c++". – Eran Zimmerman Gonen Aug 22 '11 at 20:48
  • it may seem a simple problem to convert string to wstring but Iv spent 2 days and dont get the right result – aliakbarian Aug 22 '11 at 20:52
  • See answers to this question: http://stackoverflow.com/questions/7141260/compare-stdwstring-and-stdstring/7141293#7141293 – Don Reba Aug 22 '11 at 20:54
  • I'm willing to bet that your question is actually, how do I convert utf8 to utf16? But I doubt even that's what you want. You can compare utf8 strings perfectly well if you know how to. And even that might not be your real problem. But you don't seem able to ask the question that you need to. – David Heffernan Aug 22 '11 at 20:54
  • 1
    @aliakbarian: It will be better if you post your actual problem that you're trying to solve (with the pugixml usage and what comparison you try to do). I'm almost sure you're trying to solve your problem in the wrong way. – Yakov Galka Aug 22 '11 at 20:56
  • @David: 'you don't seem able to ask the question that you need to', him and every single other newbie on this site. Agreed though utf-8 to utf-16 seems the most likely thing he's after. – john Aug 22 '11 at 21:07
  • I would edit my question but the code is actually what I post yesterday and I hope you dont punish me to ask one question several times – aliakbarian Aug 22 '11 at 21:19
  • @Alikbarian. I've looked at your code above and you are converting UTF-8 to wstring but you are not doing it the right way! Just because it compiles doesn't not mean that you are doing it right! Here's the question you need to ask, 'if i have a UTF-8 std::string how to I convert it to a UTF-16 std::wstring. Ask that question in a new post and I will personally answer it. – john Aug 22 '11 at 21:35

3 Answers3

3

You cannot, it's a part of the string-literal itself. It's not an operator.

string-literal:
    encoding-prefixopt "s-char-sequenceopt"
    encoding-prefixoptR raw-string

encoding-prefix:
    u8
    u
    U
    L

Also I recommend you to avoid using std::wstrings, unless you make a low-level windows API call.

EDIT:

If you compiled pugixml with PUGIXML_WCHAR_MODE use:

    if(it->child("WORDVALUE").child_value() == targetWord)
        cout << "found!" << endl; 

Otherwise use:

    if(it->child("WORDVALUE").child_value() == pugi::as_utf8(targetWord))
        cout << "found!" << endl; 

I recommend compiling without PUGIXML_WCHAR_MODE and changing the function to:

void wordNet::extractWordIds(std::string targetWord)
{
    // ...
    for (pugi::xml_node_iterator it = words.begin(); it != words.end(); ++it)
        if(it->child("WORDVALUE").child_value() == targetWord)
            cout << "found!" << endl; 
}

And let the caller worry about passing a UTF-8 targetWord.

Community
  • 1
  • 1
Yakov Galka
  • 70,775
  • 16
  • 139
  • 220
  • so how can I convert std::string to std::wstring, I really need it. because I want to compare two unicode strings so they must be in wstring format – aliakbarian Aug 22 '11 at 20:48
  • On the other hand wstring is exactly what you want when calling win api. – David Heffernan Aug 22 '11 at 20:49
  • 1
    @aliakbarian: you *do not* understand what is unicode. Depending on what comparison you need, either `std::string` will work, or you must use an external unicode library written by experts (see ICU). – Yakov Galka Aug 22 '11 at 20:50
  • 1
    @aliakbarian, you asked this yesterday and got two good answers. – Don Reba Aug 22 '11 at 20:57
  • @David: as I said *it depends on what comparison he wants to do*. If it's a simple lexicographical comparison, then <, ==, > will work. If he needs to handle the Unicode equivalences and collation then it's a much more complicated task. – Yakov Galka Aug 22 '11 at 20:59
  • @alikbarian: Reading the other comments on this post, possibly (and only possibly) what you need is a UTF-8 to UTF-16 conversion. That's not so difficult to write yourself, or you could find third party code to do it (Windows can do it for you, for instance). But Unicode is tricky, so I'm only making this suggestion to help you progress, it's possible you need something completely different. – john Aug 22 '11 at 21:02
  • @Don Reba: your answers are good but my problem is still unsolved – aliakbarian Aug 22 '11 at 21:12
  • 1
    @aliakbarian: that's because you don't explain what's the real problem. Also AFAIR pugixml has `pugi::as_wide` function. – Yakov Galka Aug 22 '11 at 21:14
  • 1
    @aliakbarian: repeating the same quesiton until you get an answer you like is not how StackOverflow works. – Don Reba Aug 22 '11 at 21:16
  • @Don Reba: what can I do then? – aliakbarian Aug 22 '11 at 21:23
  • @aliakbarian, edit your question to describe the problem more fully, tell people trying to help you what you need to accept an answer. – Don Reba Aug 22 '11 at 21:32
  • @aliakbarian: When you added to the question the actual code you wanted to get work, I could provide a more useful answer. See my edit. – Yakov Galka Aug 22 '11 at 21:33
1

You have to make somfunction return either a std::wstring or a wchar_t*.

If you cannot change the function return type, you'll need a conversion from string to wstring which is not something that can be done at compile time - you'll need to call a function to do it. The question has been asked many times with many different variations, here's one example: C++ Convert string (or char*) to wstring (or wchar_t*)

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • I cant, because this is not really my function, it is a function in pugixml library – aliakbarian Aug 22 '11 at 20:50
  • Then you have a bigger problem - if this function doesn't return wide strings, then it probably doesn't handle UTF-16. – Kos Aug 22 '11 at 20:52
  • @Kos: wrong. pugixml returns utf-8 std::strings. Also see http://programmers.stackexchange.com/questions/102205/should-utf-16-be-considered-harmful – Yakov Galka Aug 22 '11 at 20:54
  • 1
    @alikbarian: First you have to understand what encoding pugixml is using when it returns a string from this function. Then you have to use the approriate algorithm to convert that encoding to 'unicode'. Then you can do the comparison. It's a lot more complicated than putting 'L' in front of a function call. – john Aug 22 '11 at 20:55
  • @john: I think your idea is true. when I convert the string returned by pugixml to wstring I dont get the right answer either.. I dont know really what it is, documentation say it is utf8 but it is not! – aliakbarian Aug 22 '11 at 21:11
  • @alikbarian: How do you convert to wstring? Can you print out the byte values of the string returned by pugixml? If you can it will probably be very easy to tell if it is UTF-8 or not. – john Aug 22 '11 at 21:30
1

You can't.

You should copy the result of the string in the wstring, for instance:

std::string tmp = somefunction ();
std::wstring result (tmp.size (), L' ');
std::copy (tmp.begin (), tmp.end (), result.begin ());

From pugixml documentation:

There are cases when you'll have to convert string data between UTF-8 and wchar_t encodings; the following helper functions are provided for such purposes:

std::string as_utf8(const wchar_t* str);
std::wstring as_wide(const char* str);
Nicolas Grebille
  • 1,332
  • 8
  • 15