displaying unicode strings?

Recommended Posts

michael879    100
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: [CODE] #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; } } [/CODE] 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 ?".

Share on other sites
michael879    100
o yea I also tried just printing out L"\u2660" and got question marks.

Share on other sites
RDragon1    1205
by the way, std::basic_string<std::wchar_t> is std::wstring

Share on other sites
michael879    100
I know, for some reason that isnt defined in g++

Share on other sites
Anon Mike    1098
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.

Share on other sites
michael879    100
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?

Share on other sites
Zahlman    1682
Quote:
 Original post by michael879well 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 ;)

Share on other sites
Bregma    9214
Quote:
 Original post by michael879Im 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

Share on other sites
michael879    100
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.

Share on other sites
Niksan2    265
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.

Share on other sites
michael879    100
ok I gotcha, so what I want isnt really possible. Guess I have to live with 's','c','h','d' until I make a GUI. thanks anyway.

Share on other sites
Mastaba    761
If you want to display the characters in a Windows console window, it is very doable. By default, the console window uses a font that is compatible with the DOS font.

#include <iostream>int main(int argc, char** argv){	std::cout << "\x03\x04\x05\x06" << std::endl;	return 0;}

That prints all four card suits in a console window.

Create an account

Register a new account