Sign in to follow this  
michael879

displaying unicode strings?

Recommended Posts

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 this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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 ;)

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this