-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.

8
  • 2
    Try googling "convert string to wstring c++". Commented Aug 22, 2011 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 Commented Aug 22, 2011 at 20:52
  • See answers to this question: stackoverflow.com/questions/7141260/… Commented Aug 22, 2011 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. Commented Aug 22, 2011 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. Commented Aug 22, 2011 at 20:56

3 Answers 3

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.

Sign up to request clarification or add additional context in comments.

12 Comments

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: 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).
@aliakbarian, you asked this yesterday and got two good answers.
@aliakbarian: that's because you don't explain what's the real problem. Also AFAIR pugixml has pugi::as_wide function.
@aliakbarian: repeating the same quesiton until you get an answer you like is not how StackOverflow works.
|
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*)

6 Comments

I cant, because this is not really my function, it is a function in pugixml library
Then you have a bigger problem - if this function doesn't return wide strings, then it probably doesn't handle UTF-16.
@Kos: wrong. pugixml returns utf-8 std::strings. Also see programmers.stackexchange.com/questions/102205/…
@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: 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!
|
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); 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.