-4

I am new to c++, I am trying to print some values on console. I am using below code for this

std::cout  << "text : " << text.latin1().data() <<endl;

text.latin1().data() returns a char* to me. It works fine as long as values are normal ASCII but it prints "?" for special characters like alpha, beta and gamma (may be for utf-8).

text is WTF::String latin1 is WTF::CString and data is char*

How can I get the unicode values for the given char* and print their string values instead of "?". Thanks

Pavan Tiwari
  • 3,077
  • 3
  • 31
  • 71
  • possible duplicate: https://stackoverflow.com/questions/2492077/output-unicode-strings-in-windows-console-app – NathanOliver Apr 02 '18 at 18:11
  • 1
    What is the type of the variable `text`? – R Sahu Apr 02 '18 at 18:11
  • 2
    This is unanswerable without further details. But it seems to me that if you are asking for a Latin1 encoding then you shouldn't be surprised when your non-Latin1 characters are lost. – Lightness Races in Orbit Apr 02 '18 at 18:18
  • 2
    `latin1()` is not a part of C++. Are you using a third party text processing library? Which one? – n. m. could be an AI Apr 02 '18 at 18:20
  • @n.m. latin1() method returns WTF::CString – Pavan Tiwari Apr 02 '18 at 18:27
  • @RSahu text is WTF::String – Pavan Tiwari Apr 02 '18 at 18:28
  • @PavanTiwari, I have never used `WTF::String`. Hopefully someone else can find a satisfactory answer for you. Good luck. – R Sahu Apr 02 '18 at 18:31
  • @RSahu eventually i am getting char* from the method call, so you might have used char* ,so is there any way to print unicode from char*. – Pavan Tiwari Apr 02 '18 at 18:33
  • You'll probably need to do something with `std::wcout`; I don't think you can print unicode chars with `std::cout` – Justin Apr 02 '18 at 18:34
  • @Justin I have tried both but none of them are working for me – Pavan Tiwari Apr 02 '18 at 18:36
  • 1
    @PavanTiwari, Not all unicode characters can be represented by a `char`. Unless `WTF::String` has the ability to return a unicode character given an index, you might be stuck. But then, I am guessing. There might be ways that I am not aware of since I have not used `WTF::String`. – R Sahu Apr 02 '18 at 18:37
  • 2
    WTF is WTF? sorry, this had to be asked. – n. m. could be an AI Apr 02 '18 at 18:37
  • If you were to call `text.containsOnlyLatin1()`, you'll see that it returns `false` – Justin Apr 02 '18 at 18:38
  • @n.m. https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/text/WTFString.cpp link for WTF:STRING i am working on phantomjs code base – Pavan Tiwari Apr 02 '18 at 18:39
  • @Justin https://github.com/WebKit/webkit/blob/master/Source/WTF/wtf/text/WTFString.cpp code of WTF:String – Pavan Tiwari Apr 02 '18 at 18:40
  • 1
    "new to c++" "webkit" I don't think this is a winning combo, but what do I know. C++ doesn't support Unicode well. There is no standard way to print Unicode on the console and make it readable. On Linux this usually just works when you use UTF-8, possibly unless you are root, or on something old, or on something embedded, or on something proprietary, in which case good luck. On Windows, just good luck, regardless. – n. m. could be an AI Apr 02 '18 at 18:46

1 Answers1

0
std::wcout << L"text : " << text.characters() << wendl;

Does it work ? If not what's the compiler error message ?