displaying unicode strings?

Started by
10 comments, last by Mastaba 17 years ago
Im making this poker program in C++, and Im trying to make it display the suits using the unicode symbols (2660-2668). Im running this in putty, and Im having some trouble. Im not sure whether its the code or putty's character set. Ive tried various different ways of doing this but I think this one is the one that should work: #include <iostream> #include <string> #include <locale> using namespace std; int main() { for(unsigned int i = 2660; i < 2670; i++) { basic_string<wchar_t> str(L"0"); str[0] = (wchar_t)i; wcout << i << L"\t" << str << endl; } } however all i get is question marks as output. I changed puttys character set to be UTF-8, so unless Im missing something my codes probably wrong. Any help would be great. Also, is there some way to check if its possible to output these symbols or not? I would rather have "5 spades" than "5 ?".
Advertisement
o yea I also tried just printing out L"\u2660" and got question marks.
by the way, std::basic_string<std::wchar_t> is std::wstring
I know, for some reason that isnt defined in g++
Sounds like either the console code is playing games with the character stream trying to fit it into the current locale or the font it is using simply doesn't support the characters you are trying to print.

I don't know how *nix works in this regard. I know on Windows the console is fairly limited in these things because it has to be compatible with all the 500 billion different character sets that existed pre-Unicode.
-Mike
well Id be happy if I could just find a way to print out using the DOS character set. Anyone know how to do that?
Quote:Original post by michael879
well Id be happy if I could just find a way to print out using the DOS character set. Anyone know how to do that?


You might want to take a look at how the console displays the first few ("nonprintable") ASCII characters by default ;)
Quote:Original post by michael879
Im making this poker program in C++, and Im trying to make it display the suits using the unicode symbols (2660-2668). Im running this in putty, and Im having some trouble. Im not sure whether its the code or putty's character set. Ive tried various different ways of doing this but I think this one is the one that should work.... I changed puttys character set to be UTF-8, so unless Im missing something my codes probably wrong. Any help would be great. Also, is there some way to check if its possible to output these symbols or not? I would rather have "5 spades" than "5 ?".


Those characters would be from which Unicode set, UTF-16 (or Microsoft's proprietary variant)? I would think that if you're putting out wide characters to a wide stream you would want to examine them using a wide character viewer. UTF-8 is a multibyte character set. I would expect to see garbage given your description.

Here's some suggestion to try (I haven't tried any of the, I don't have access to a Windows machine).

(1) Try using wchar_t as the type of i -- maybe there's a byte ordering problem (always possible with wide characters).

(2) Try using a narrow stream instead. The stream should convert your wide characters to multibyte characters automatically, assuming you've set the locale correctly.

(3) Make sure the output stream is imbued with a locale that supports UTF-8 output.

(4) If you're using MinGW on Windows (just a guess) be aware that wide character support doesn't work. Try using another compiler+library (MSVC?).

(5) Redirec the output into a file and hexdump the file -- maybe you can get a clue (byte ordering problems, etc).

Hopefully one of these suggestoins might reveal a useful angle for attacking your problem.

--smw

Stephen M. Webb
Professional Free Software Developer

ok thats way too complicated. Is there any way to cout using the DOS character set? That way Id still be using 1 byte characters.
As already mentioned, you need to make sure your client is using the correct font, as essentially you're telling the client to draw a nice big green tree, but the client doesn't have that graphic.

This topic is closed to new replies.

Advertisement